Spring IoC Containers and Dependency Injection

The core part of Spring Framework is Container. The Container is responsible to create the objects, configure objects, and wire objects in other whenever is required. It also manages the life cycle of bean. It avoid writing unnecessary creation and lookup code in application. The Spring framework uses Dependency Injection (DI) and injects the object into the other object during runtime. The Dependency Injection promotes loose coupling. The Dependency Injection is alse known as Inversion on Control (IoC). The IoC refers to the control of creating instances being done by the Spring Container. The container creates objects and injects them into application. You don't directly connect your components and services together in code but describe which services are needed by which components in a spring configuration file.

What does IoC do:

  • Create new objects
  • Configure/solve dependency among objects and assemble them
  • Allow objects to be retrieved by id/name
  • Manage object’s lifecycle
  • Allow external configuration

Why do we use IoC ?

  • Reduce the amount of code in your application
  • Does the plumbing work for you
  • Application is more testable
  • No more reading and processing of configuration files
  • No more creating and hooking of objects together
  • No more lookup

The following are the cases where dependency injection cab ne used in enterprise applications:

  • The DAO classes use the data Source which can be injected into them
  • The Service classes may need to consume few enterprise java beans
  • The objects may have to load the message resources to use the messages
  • One object is dependent on other object at runtime

Spring Container

Spring provides 2 types of IoC container implementation :

BeanFactory  (Basic)

  • Interface that represents Spring’s IoC Container
  • An implementation of the factory design pattern
  • Function is to create, configure, assemble and destroy beans
  • Many implementations of BeanFactory are available.  Commonly used one is XmlBeanFactory
  • Instructions for managing beans (Configuration Metadata) can be given in
    • XML
    • Java Annotations (From Spring 2.5)
    • Java code (From Spring 3.0)

ApplicationContext (Advanced):

  • ApplicationContext Interface extends BeanFactory Interface
  • BeanFactory provides the configuration framework and basic functionality
  • ApplicationContext adds more enterprise-specific functionality like e-mail, JNDI access, EJB integration, remoting and scheduling
  • Also offers the below features at the same cost :
    • I18n Message resource handling
    • Event publication
    • Web application context
    • Loading file resources in a generic fashion
ApplicationContext implementation : Several ways to configure a context:

  • XMLWebApplicationContext :Configuration for a web application
  • ClassPathXMLApplicationContext :Loads  context definition from an XML file in the classpath
  • FileSystemXmlApplicationContext :Loads context definition from an XML file in the Filesystem
  • XMLPortalWebApplicationContext :Configuration for a Portal application
The Container can be created either XML based or Annotation based.
XML based container :
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");

Multiple XML file based Container :
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {"beans1.xml", "beans2.xml"});


Previous Next


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code