Category Archives: Spring

Spring Dependency Injection

Spring DI is the most fundamental feature that it provides. This feature has also been referred as Inversion of Control but after many discussions it was decided that DI is a better term.

Any application which is of practical use is composed of two or more Objects which communicate or use each other.  Traditionally each of those objects is responsible to get the reference of objects it uses but in DI objects are given references to the other objects when needed. All the bean creation and their injection are handled by Spring which acts as a container of beans. This provides loose coupling and also promotes programming to an Interface as the bean knows only about the type of bean but nothing about its implementation.

For Example: If the bean Store has declared Product property as an interface then the Product implementation becomes irrelevant.   The Product bean can be web-service implementation, an EJB or a mock test object.

Some other benefits of using DI:
• Reduction of boiler plate coding.
• As all metadata is in xml file and is easy to configure. This gives flexibility in testing and development.

Leave a comment

Posted by on July 5, 2010 in Java, Spring


Tags: ,

Java Example of Spring Dependency Injection

This tutorial uses the following:

    • Eclipse IDE
    • Spring Jars
    • commons-logging-api-1.1.jar
  1. Make a new Java Project in Eclipse.
  2. Add the following jars to its classpath:
    • commons-logging-api-1.1.jar
    • org.springframework.core-3.0.2.jar
    • org.springframework.beans-3.0.2.jar

The Spring jars can be taken from the dist folder of the expanded zip file. Here you have to include the two jars one for core and other for beans. Commons-logging is used for logging purpose.

  • Make a new class – It is a 100% Java bean and has the proper getters and setters.
package com.spring.ioc.beans;

public class Person {

	private String name;

	private String address;

	 * @return the name
	public String getName() {
		return name;

	 * @param name the name to set
	public void setName(String name) { = name;

	 * @return the address
	public String getAddress() {
		return address;

	 * @param address the address to set
	public void setAddress(String address) {
		this.address = address;


  • Make an XML file and name it context.xml and save it in the src folder.  This file contains all the meta information needed for bean creation. Here the bean id is person and class has the full qualified name of the class. The “name” property of the class Person is populated with the value Mac.

<?xml version="1.0" encoding="UTF-8"?>
	xmlns:xsi="" xmlns:p=""
	<bean id="person" class="com.spring.ioc.beans.Person">
	<property name="name" value="Mac"></property>

  • Make another class named  Here an object of XmlBeanFactory loads the configurations from contect.xml file. Also its constructor takes an argument of ClassPathResource.
  • To get a bean from XmlBeanFactory object use its reference type to invoke the method getBean(<bean id>) and it will return the bean as defined in the spring context file.  The Object it returns is of type Object so you do have to cast it to the correct type.
package com.spring.ioc.main;

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

import com.spring.ioc.beans.Person;

 * @author hgupta
public class Main {

	 * @param args
	public static void main(String[] args) {
		XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
		Person person = (Person) beanFactory.getBean("person");


  • Run the Main class as Java Application and you will see the following output: The output is the name of the person which you configured in the xml file.


May 27, 2010 6:54:04 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

INFO: Loading XML bean definitions from class path resource [context.xml]


Leave a comment

Posted by on July 5, 2010 in Java, Spring


Tags: ,

Spring Framework Architecture

Spring framework is layered in architecture which consist several modules. All its modules are built on the top of its core container. These modules provide everything that a developer may use in the enterprise application development. He is always free to choose what all features he needs and eliminate the modules which are of no use. Its modular architecture enables integration with other frameworks without much hassle.

The Core Module: It provides the Dependency Injection (DI) feature which is the basic concept of spring framework. This module contains the BeanFactory an implementation of Factory Pattern which creates the bean as per the configurations provided by the developer in an XML file.

AOP Module: Its Aspect Oriented Programming module allows developer to define method-interceptors and point cuts to keep the concerns apart. It is configured at run time so the compilation step is skipped. It aims at declarative transaction management which are easier to maintain.

DAO Module: This provides an abstraction layer to the low level task of creating a connection, releasing it etc. It also maintains a hierarchy of meaningful exception rather than throwing complicated error codes of specific database vendors. It uses AOP to manage transactions. Transactions can also be managed programmatically.

ORM Module: Spring doesn’t provides its own ORM implementation but offers integrations with popular Object Relational mapping tools like Hibernate, iBATIS SQL Maps, Oracle TopLink and JPA etc.

JEE Module: It also provides support for JMX, JCA, EJB and JMS etc. In lots of cases, JCA (Java EE Connection API) is much like JDBC, except where JDBC is focused on database JCA focus on connecting to legacy systems.

Web Module: Spring comes with MVC framework which eases the task of developing web applications. It also integrates well with the most popular MVC frameworks like Struts, Tapestry, JSF, Wicket etc.

Leave a comment

Posted by on July 5, 2010 in Java, Spring


Tags: , ,