Java
  Home arrow Java arrow Page 2 - The Spring Framework: Understanding IoC
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? 
Google.com  
JAVA

The Spring Framework: Understanding IoC
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 15
    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:
      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


    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
     

       

    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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek