Spring – Dependency Injection using Inversion of Control

What is Dependency Injection ?

“Dependency Injection”, the word itself clarifies that we are trying to Inject the dependency between the Parent Classes and the calling Class.  But how spring provides it as a Feature and how it is different from previous implementation, is what we need to understand.

Java Programming Concept is all about the object oriented programming, class instantiation, encapsulation and polymorphism. Spring also executes on the same implementation but it provides a simple approach where it allows to make a class independent from its dependencies. To achieve this and make classes loosely decoupled, Spring separates the class creation and class usage (invoked by another dependent class).

Dependency Injection is implemented by using the technique termed as ‘Inversion of Control’. First, we need to understand how IoC Container uses the Spring Framework.

How IoC Container uses the Spring Framework ?

Core Packages: The org.springframework.beans and org.springframework.context packages are the Core Component for Spring Framework IoC Container execution.

Bean Factory: The advanced configuration capability comes from the usage of the BeanFactory. It acts as a Factory class and provides Bean Definition for the multiple application beans and create instances of the bean whenever is requested by the client call.   

Application Context: The ApplicationContext provides access to the bean methods, loading file resources, publishing events, message resourcing supporting internationalization, parent context inheritance for easy integration.

Application Context implementatio n

The ApplicationContext provides the below given commonly used implementation:

  1. ClassPathXmlApplicationContext:  loads the context definition for the application by retrieving it from the XML File.
<strong>ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");</strong>
  • FileSystemXmlApplicationContext: loads the context definition for the application by retrieving it from the XML File placed in the File System.
<strong>ApplicationContext context = new FileSystemXmlApplicationContext("bean.xml");</strong>
  • XmlWebApplicationContext: loads the context definition for the application by retrieving it from the XML File within the web application.
<strong>&nbsp;ApplicationContext context = new XmlWebApplicationContext ("bean.xml");</strong>

Below given are the possible ways for implementing the Inversion of Control (Spring IoC Container):

  1. With the usage of Factory Pattern
  2. With the usage of Service Locator Pattern
  3. With the Inclusion of Dependency Injection through
  • The Constructor Injection
  • The Setter Injection
  • The Interface Injection

Dependency Injection – using Constructor Injection

Spring – Bean Scope

The most important component of the Spring Framework is the Spring Core Container which creates the objects, wires them together, manages the configuration and overall object life cycle from creation of the object till its destruction. These objects are called Beans which are created from the configuration metadata provided to the container. Configuration Metadata provides the information to the container with regard to bean

  1. Managing Bean Creation Process

2. Managing life cycle of the bean

3. Managing dependencies of the bean

Configuration Metadata Properties

Below are the commonly used properties for the configuration metadata in spring bean scope.

Name – specifies bean identifier

Class –  specifies the bean class for which bean is to be created

Constructor –arg – specifies the dependency injection  

Scope – specifies the scope of the object created from the bean definition

Properties – specifies properties for the dependency injection  

Auto wiring Mode – specifies the dependency injection  

lazy-initialization mode – specifies the lazy initiation of the bean instance. That is, bean instance is created when it is requested and not at the start-up of the application

initialization method – A callback to be called just after all necessary properties on the bean have been set by the container

destruction method – A callback to be used when the container containing the bean is destroyed

Configuration Metadata Methodologies

Configuration Metadata can be provided by using the below given methodologies

  • XML based configuration file.
  • Annotation-based configuration
  • Java-based configuration

Sample XML Configuration

<?xml version = "1.0" encoding = "UTF-8"?>

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans

   <!-- A simple bean definition -->
   <bean id = "..." class = "createCustomer">

   <!-- A bean definition with lazy init set on -->
   <bean id = "..." class = "..." lazy-init = "true">

   <!-- A bean definition with initialization method -->
   <bean id = "..." class = "..." init-method = "Initiate-customer-process">

   <!-- A bean definition with destruction method -->
   <bean id = "..." class = "..." destroy-method = "destruct-customer-process">


Spring supported Bean Scope

The Spring Framework supports the below given bean scopes.

Singleton Scope:  With the use of singleton scope, Spring IOC Container creates only 1 bean instance of the object defined by the bean definition.

The below bean configuration file does not include the scope specified and thus makes it as a singleton instance.


Another bean configuration with the usage of scope as “singleton”


Prototype Bean Scope

With the use of prototype scope, Spring IOC Container creates a new bean instance every time the bean is requested.

Spring Prototype Bean Scope

Bean Scope Annotation

Spring also provides annotation to define the bean scope as an alternative to the XML based configuration. The annotation based bean depends on bytecode metadata for wiring up components instead of angle-bracket declarations and allows developers to write the configuration into the component class itself by using annotations on the relevant class, method, or field declaration.

The RequiredAnnotationBeanPostProcessor, using a BeanPostProcessor in conjunction with annotations is a common means of extending the Spring IOC container.

Spring 2.5 also added support for JSR-250 annotations, such as @PostConstruct and @PreDestroy.

Spring 3.0 added support for JSR-330 (Dependency Injection for Java) annotations contained in the javax.inject package such as @Inject and @Named

Spring Bean Scope Annotation