Java
  Home arrow Java arrow Page 3 - Introducing the Spring Framework
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 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: starstarstarstarstar / 18
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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

     
     
    ADVERTISEMENT


    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
     

       

    JAVA ARTICLES

    - Exception Handling Techniques in Java
    - More About Multithreading in Java
    - The Basics of Multiple Threads in Java
    - Data Access Using Spring Framework JDBC
    - New Object Initialization in Java
    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - 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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT