Java
  Home arrow Java arrow Page 3 - Introducing the Spring Framework
FaxWave - Free Trial.
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Introducing the Spring Framework
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 9
    2008-03-05

    Table of Contents:
  • Introducing the Spring Framework
  • Using Spring Framework, Step by Step
  • Mapping the Beans continued
  • Spring Framework in the Real World
  • Beans and Client

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    Iron Speed
     
    ADVERTISEMENT

    Ziff Davis Enterprise Virtual Tradeshows: Hot Topics, Cutting Edge Technology, Real-time Interaction with IT Professionals. Learn more at ziffdavisvts.com

    Introducing the Spring Framework - Mapping the Beans continued
    (Page 3 of 5 )

    Property is the child tag of the bean tag. It is used to provide the details of the property of the bean that needs to be accessed or populated. Property is the member variable of the bean that has been mapped using a bean tag. It is done using the name attribute of the property tag. To access the greeting property of the SimpleInterest bean, the property tag will be:


    <beans>

    <bean id=”Hello” class=”org.me.Hello”>

    <property name=”greeting”>

    :

    </property>

    </bean>

    <beans>


    The value tag is the child of property tag. The value to be given to the property is passed as the child node of the value tag. To pass Good Morning to the greeting property as the value, the code will be:


    <beans>

    <bean id=”Hello” class=”org.me.Hello”>

    <property name=”greeting”>

    <value>Good Morning<value>

    </property>

    </bean>

    <beans>


    Next, we will see what is required to implement the client.

    Developing the Client: Clients need to do the following to access the Spring Framework service:

    • Instantiate the configuration file as a resource
    • Instantiate an implementation of BeanFactory
    • Call the business method

    Here are the details.

    The first step is to instantiate the configuration file as a resource. To access the business methods, first the configuration file needs to be passed to the framework. Spring accepts the configuration file as an instance of the Resource interface’s implementation class. The most commonly used implementation class is ClassPathResource. It is instantiated using the name of the configuration file. For example, to create an instance of ClasspathResource with the filename beans.xml and assign it to the reference of the Resource interface, the statement will be:

    Resource resource=new ClassPathResource(“beans.xml”);

    Now we are ready for the second step. To access the beans populated with values, the second step is to get a reference for the BeanFactory interface. The reference would contain the instance of an implementation of the BeanFactory interface. The commonly used implementation is XmlBeanFactory. To get an instance of XmlBeanFactory, the reference of the Resource interface has to be passed to the constructor of XmlBeanFactory. For example, to get an instance of XmlBeanFactory and assign it to a reference of BeanFactory, the statement will be:

    BeanFactory factory=new XmlBeanFactory(resource);

    Finally, to call the business methods, the instance of bean needs to be retrieved using the getBean() method of BeanFactory. The method return Object instance needs to be typecast into the POJO class. Once that is done, the business objects can be called. In code, it will be:


    Hello hello=( Hello)factory.getBean(“Hello”);

    System.out.println(hello.sayHello());


    That completes the steps for using the Spring Framework. Next, let us look at a real world example.

    More Java Articles
    More By A.P.Rajshekhar


       · I am happy that there finally is a simple Spring tutorial out there. However, I was...
       · HiYou can remove the exception by adding jar of apache commons-2 in the class...
       · It does actually solve the problem.Good luck :)
     

       

    JAVA ARTICLES

    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup
    - Using RPC-Style Web Services with J2EE
    - Integrating XML with J2EE
    - Taming Tiger: Concurrent Collections
    - Combating the ‘Object Crisis’

    Iron Speed



    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway