Java
  Home arrow Java arrow Page 2 - The Spring Framework: Understanding Io...
Dev Shed Forums 
Administration  
AJAX  
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 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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

The Spring Framework: Understanding IoC
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2008-05-14

    Table of Contents:
  • The Spring Framework: Understanding IoC
  • Spring Framework and Forms of IoC: Implementation
  • Injections
  • Spring Framework in the Real World

  • 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
     
     
    ADVERTISEMENT


    The Spring Framework: Understanding IoC - Spring Framework and Forms of IoC: Implementation


    (Page 2 of 4 )

    The central aspect of the Spring Framework is the configuration file, as all the details of the POJOs as well as connecting them with the required services is done in the configuration file. Using different forms of IoC is also no exception. The declarations in the configuration file decide which form of Dependency Injection the container uses.

    This section will focus on the required declarations in the configuration file. The POJO that will be used throughout this section will be a simple greeting bean that will get the user’s name and return the string “Greeting” added to the user name. The GreetingBean class is as follows:


    public class GreetingBean

    {

    private String userName;

    public GreetingBean()

    {


    }

    public GreetingBean(String a)

    {

    userName=a;

    }

    public String sayhello()

    {

    return “Greetings “+userName;

    }

    public void setUserName(String a)

    {

    userName=a;

    }

    }

    Now let us see how to use the different forms of Dependency Injection with Spring Framework.


    Constructor Injection

    To use constructor injection, the required declaration is done in the bean section of the configuration file that is the beans.xml file. The steps to use Constructor Injection are setting the constructor declaration and passing the value to the constructor.

    Setting the constructor declaration tells the container that the application wants to use the Constructor Injection. The declaration is done using the <constructor-arg> child element of <bean> element. For example, to use the Constructor Injection for a POJO named GreetingBean the code would be:


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

    <constructor-arg>

    :

    :

    </constructor-arg>

    </bean>


    The next step is to pass the value to the constructor. To do this, the <value> element of the <constructor-arg> has to be used. The value to be passed is given as the value of the <value> element. For example, to pass “Raj” as username, the code would be: 

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

     <constructor-arg>

    <value>Raj</value>

    </constructor-arg>

    </bean>


    That completes the steps for using Constructor Injection. Next we will look at how to use the Setter Injection.

    More Java Articles
    More By A.P.Rajshekhar


       · Thanks for providing good article for understading basics of Spring IOC
       · Great article. I can't wait for the next one...Just a few questions. The...
     

       

    JAVA ARTICLES

    - 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...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming





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