Java
  Home arrow Java arrow Page 4 - 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 in the Real World
    ( Page 4 of 4 )

    The application I will develop in this section is a Compound Interest calculator. The values to be calculated will be "injected" via Constructor Injection. The application consists of three files:

    1. CompoundInterestBean – The Java class with the compound interest logic.

    2. beans.xml – The configuration file for Spring.

    3. Client – The client class that calls the CompoundInterestBean.


    Let's start with the class for the CompoundInterestBean. Just as the name suggests, it is a bean with setters and getters for Principle, Rate and Time. There are two extra methods: calculate, that calculates the interest; and getInterest, that returns the interest calculated.


    package org.me;

    class CompoundInterestBean {


    float years;

    float principle;

    float rate;


    CompoundInterestBean(){

    }


    public void setYears(float years){

    this.years=years;

    }


    public float getYears(){

    return years;

    }

     

    public void setPrinciple(float principle){

    this. principle = principle;

    }


    public float getPrinciple(){

    return principle;

    }

     

    public void setRate(float rate){

    this. rate=rate;

    }

     

    public float calculate(){

    return (float)((principle*(Math.pow(1+(rate/100)),time))-1);

    }


    public float getInterest(){

    return calculate();

    }

     

    }


    Next comes the beans.xml. It contains the declarations for the constructor injection. Since there is more than one argument, there are multiple <constructor-arg> elements.


    <beans>

    <bean id=”CompoundInterestBean”

    class=”org.me. CompoundInterestBean”>

    <constructor-arg>

    <value>10000.00<value>

    </constructor-arg >

    <constructor-arg >

    <value>10.00<value>

    </ constructor-arg >

    <constructor-arg>

    <value>9.50<value>

    </constructor-arg >


    </bean>

    <beans>


    Next is the client class that calls the bean to calculate the interest.


    import java.io.*;

    import org.springframework.beans.factory.*;

    import org.springframework.beans.factory.xml.*;

    import org.springframework.core.io.*;


    public class Client

    {

    public static void main(String args[]) throws Exception

    {

    try

    {

    System.out.println("please Wait.");

    Resource res = new ClassPathResource("beans.xml");

    BeanFactory factory = new XmlBeanFactory(res);

    CompoundInterestBean interest=

    (CompoundInterestBean)factory.getBean(“CompoundInterestBean”);


    System.out.println(interest.getInterest());

    }


    catch(Exception e1)

    {

    System.out.println(""+e1);

    }

    }

    }


    To run this application successfully you will need the Apache Commons Library. 

    That completes this discussion on how to use the forms of IoC with the Spring Framework. However, it opens up certain other questions. For example, can the IoC work for accessing data from database or other data sources? These questions will be the focus of discussion in the next part. Till then…



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