Step by Step understanding of Spring IOC.
What is IOC?
Inversion of control by definition is a process whereby objects define their dependencies, that is, the other objects they work with, only 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. The container then injects those dependencies when it creates the bean. This process is fundamentally the inverse, hence the name Inversion of Control (IoC), of the bean itself controlling the instantiation or location of its dependencies by using direct construction of classes, or a mechanism such as the Service Locator pattern. (Source:- Spring documentation about the ioc container)source:- Spring documentation.
when the spring container is created then it looks for all the beans(classes) in the config xml and then injects the properties at run time once application is started. This is against the normal flow which is when you create a class with properties then these are created during the compilation time. Here we are providing or injecting these properties during the runtime. This is called as Inversion of control.
How is it helpful in enterprise applications?
Most of the current enterprise applications which are dealt with large teams and so many different modules the ioc is very widely used. Here the common class for example the database connection factory is just created once with all the properties and then defined in the config xml. Once the application starts and the spring container is created this will be ready to be used in multiple classes.
What is autowiring?
Autowiring is the way you link the class A with class B and use all the capabilities of class A in class B. In normal terms this is used to link the beans defined in the configuration xml.
Types of IOC?
There are three types of dependency injection:
· Constructor Injection (e.g. Pico container, Spring etc): This will injects the dependency via a constructor.
· Setter Injection (e.g. Spring): This is the most popular it will injects the dependency via a setter method.
· Interface Injection (e.g. Avalon): Injection is done through an interface.
Note: Spring supports only Constructor and Setter Injection
Note:- Mostly widely used injections are Setter injections and constructor injection.
SetterInjection:-
In the class
In the class HelloWorld{
Helloworld2 helloworld2;
public void sethelloWorld2(Helloworld2 helloworld2){
this.helloworld2= helloworld2;
}
}
In the xml file
<bean id="Helloworld" class="com.test.Helloworld">
<property name="helloworld2" ref="helloworld2" />
</property>
</bean>
<bean id="helloworld2" class="com.test.Helloworld2">
ConstructorInjection:-
In the class HelloWorld{
Helloworld2 helloworld2;
HelloWorld(Helloworld2 helloworld2){
this.helloworld2= helloworld2;
}
}
In the xml file
<bean id="Helloworld" class="com.test.Helloworld">
<constructor-arg>
<bean class="com.test.Helloworld2" />
</constructor-arg>
</bean>
Now lets see a small example
Step1:- Create a normal spring + maven project.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.test.spring</groupId>
<artifactId>SpringDependencyInjection</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringDependencyInjection</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<spring.version>3.2.3.RELEASE</spring.version>
</properties>
</project>
Step2:- Create classes HelloWorld , HelloWorld2 and EmployeeDetails
package com.test.springdep;
public class EmployeeDetails {
private String name;
private String telephoneNumber;
private int age;
public EmployeeDetails(String name, String telephoneNumber, int age){
this.name = name;
this.telephoneNumber = telephoneNumber;
this.age = age;
}
public EmployeeDetails(String name, int age, String telephoneNumber){
this.name = name;
this.age = age;
this.telephoneNumber = telephoneNumber;
}
public String toString(){
return " name : " + name+ " \n telephoneNumber : " + telephoneNumber + "\n age : " +age;
}
}
package com.test.springdep;
public class HelloWorld2 {
private EmployeeDetails employeeDetails;
//Constructor injection
public HelloWorld2(EmployeeDetails employeeDetails){
this.employeeDetails = employeeDetails;
}
public String getEmpDetails(){
return employeeDetails.toString();
}
//Setter injection
public void setEmployeeDetails(EmployeeDetails employeeDetails) {
this.employeeDetails = employeeDetails;
}
}
package com.test.springdep;
public class HelloWorld {
private HelloWorld2 helloWorld2;
//Constructor injection
public HelloWorld(HelloWorld2 helloWorld2) {
super();
this.helloWorld2 = helloWorld2;
}
//Setter injection
public void setHelloWorld2(HelloWorld2 helloWorld2) {
this.helloWorld2 = helloWorld2;
}
}
Step3: Define the configuration file.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation=" http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd">
<bean id="helloWorldBean"
class="com.test.springdep.HelloWorld">
<constructor-arg ref="employeeDetails" />
</bean>
<bean id="helloWorldBean2"
class="com.test.springdep.HelloWorld2">
<constructor-arg ref="employeeDetails" />
</bean>
<bean id="employeeDetails" class="com.test.springdep.EmployeeDetails">
<constructor-arg>
<value>employeename</value>
</constructor-arg>
<constructor-arg>
<value>25</value>
</constructor-arg>
<constructor-arg>
<value>harsha</value>
</constructor-arg>
</bean>
<!-- setter injection -->
<bean id="helloWorldBean"
class="com.test.springdep.HelloWorld">
<property name="helloworldBean2" ref="helloWorldBean2" />
</bean>
</beans>
Step4:- Now write a standalone class to test the injections.
package org.test.spring.SpringDependencyInjection;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.test.springdep.HelloWorld;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorldBean");
System.out.println(helloWorld.getHelloWorld2().getEmpDetails());
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.test.springdep.HelloWorld;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorldBean");
System.out.println(helloWorld.getHelloWorld2().getEmpDetails());
}
}
Note:- We are getting the helloworldbean which is our HelloWorld Class and then in that we will be calling the helloworldbean2 class and which has a property of employeedetails and was injected with values during the runtime.
HAPPY LEARNING!!!
Please leave a comment if this was helpful.
HAPPY LEARNING!!!
Please leave a comment if this was helpful.



No comments:
Post a Comment