Java
  Home arrow Java arrow Page 4 - 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 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…


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 6 hosted by Hostway