Tuesday 24 January 2017

Spring: Inversion of Control

Inversion of control (IOC) is also known as Dependency Injection.

What is Dependency Injection?
Dependency Injection is a process, where objects define their dependencies. Suppose an object ‘obj1’ is using other objects obj2, obj3. In the source code, ‘obj1’ mention its dependencies obj2, obj3 through constructor arguments,  arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method.

What are the basic packages of Spring IOC?
a.   org.springframework.beans
b.   org.springframework.context

Spring IOC Container
org.springframework.context.ApplicationContext represents Spring IOC container and is responsible for creating and managing the beans. ApplicationContext class uses configuration meta data, to manage these beans. You can supply this configuration metadata using any configuration file (like keeping the POJOs information in xml files), (or) by using Java Annotations.

Hello World Application
 Suppose there is a POJO class that represent an Employee information. I am going to explain how to configure the metadata of Employee class in configuration file (myConfiguration.xml) and get the bean of Employee class using Spring IOC container.

Employee.java
package com.sample.pojo;

public class Employee {
 private int id;
 private String firstName;
 private String lastName;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Employee [id=").append(id).append(", firstName=").append(firstName).append(", lastName=")
    .append(lastName).append("]");
  return builder.toString();
 }

}

myConfigurations.xml
<?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
        http://www.springframework.org/schema/beans/spring-beans.xsd">

 <bean id="employee1" class="com.sample.pojo.Employee">
  <property name="id" value="12345" />
  <property name="firstName" value="Hari Krishna" />
  <property name="lastName" value="Gurram" />
 </bean>

</beans>

‘id’ attribute is used to identify a bean. The class attribute uses the fully qualified name of the class. All the properties of the bean are initialized by using property tag.

         <bean id="employee1" class="com.sample.pojo.Employee">
                 <property name="id" value="12345" />
                 <property name="firstName" value="Hari Krishna" />
                 <property name="lastName" value="Gurram" />
         </bean>


HelloWorld.java
package com.sample.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sample.pojo.Employee;

public class HelloWorld {
 public static void main(String args[]) {
  ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "myConfiguration.xml" });

  Employee employee = context.getBean(Employee.class);

  System.out.println(employee);

  ((ClassPathXmlApplicationContext) context).close();
 }
}

Run HelloWorld.java, you can able to see following output.

Employee [id=12345, firstName=Hari Krishna, lastName=Gurram]

Instantiating a container
Following statement is used to instantiate Spring IOC container.

ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "myConfiguration.xml" });

Above statement loads the configuration metadata from the file ‘myConfiguration.xml’ file. You can supply as many configuration files as you want. ClassPathXmlApplicationContext look for the configuration files in application class path.

What is advantage of Dependency injection?
a.   Decoupling of the dependencies makes your application more effective while in implementation and maintenance phases

b.   Code is cleaner.




Previous                                                 Next                                                 Home

No comments:

Post a Comment