Home arrow Java & J2EE arrow Page 4 - The Spring Framework: Understanding IoC

Spring Framework in the Real World - Java

IoC or Inversion of Control is one of the core features of Spring. It helps in simplifying the implementation of business logic. To use the Spring Framework to its full potential, understanding the IoC container of the framework is essential. Hence, in this discussion, the focus will be on the IoC – the concept as well as the container provided by Spring Framework.

  1. The Spring Framework: Understanding IoC
  2. Spring Framework and Forms of IoC: Implementation
  3. Injections
  4. Spring Framework in the Real World
By: A.P.Rajshekhar
Rating: starstarstarstarstar / 62
May 14, 2008

print this article



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;



public void setYears(float 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.


<bean id=”CompoundInterestBean”

class=”org.me. CompoundInterestBean”>



</constructor-arg >

<constructor-arg >


</ constructor-arg >



</constructor-arg >



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




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

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

BeanFactory factory = new XmlBeanFactory(res);

CompoundInterestBean interest=




catch(Exception 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 & J2EE Articles          >>> More By A.P.Rajshekhar

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates


Dev Shed Tutorial Topics: