In this blog I am going to explain you about the Spring Framework implementation of the Inversion of Control (IoC), which is also known as Dependency Injection (DI). DI is a process in which objects define their dependencies and the other objects they work with, only via constructor arguments, arguments to a factory methods or properties that are set on the object instance after it is constructed or returned from a factory method. The container injects those dependencies when it creates the bean. DI exists in two variants, Constructor-based dependency injection and Setter-based dependency injection.
Create maven project in your eclipse and follow the below steps
Step: 1 Add Spring dependencies in pom.xml file.
<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>findnerd.snippets.enterprise</groupId>
<artifactId>springexample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<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>
</dependencies>
<properties>
<spring.version>3.2.3.RELEASE</spring.version>
</properties>
</project>
Step: 2 Constructor-based dependency injection
Step: 2.1 Create Spring beans
HelloWorld.java:
package findnerd.snippets.enterprise.services;
/**
* Programmer : Bhagwan Singh
*/
public class HelloWorld {
/** Dependency on Foo class. */
private Foo foo;
/** a constructor so that the Spring container can 'inject' a Foo*/
public HelloWorld(Foo foo){
this.foo = foo;
}
public String toString(){
return " HelloWorld! foo : \n " + foo;
}
}
Foo.java:
package findnerd.snippets.enterprise.services;
/**
* Programmer : Bhagwan Singh
*/
public class Foo {
private String name;
private String telephoneNumber;
private int age;
public Foo(String name, String telephoneNumber, int age){
this.name = name;
this.telephoneNumber = telephoneNumber;
this.age = age;
}
public Foo(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;
}
}
Step: 2.2 Add xml configuration
applicationContext.xml:
<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="findnerd.snippets.enterprise.services.HelloWorld">
<constructor-arg ref="fooBean" />
</bean>
<bean id="fooBean" class="findnerd.snippets.enterprise.services.Foo">
<constructor-arg>
<value>fooname</value>
</constructor-arg>
<constructor-arg>
<value>100</value>
</constructor-arg>
<constructor-arg>
<value>25</value>
</constructor-arg>
</bean>
</beans>
When a simple type is used in the bean, such as int, Spring cannot determine the type of the value, and so cannot match by type without help. For example, in fooBean definition, the values set for the first constructor might be used from the second one as well, since the value 100 can be converted either to String or to int. In order to avoid such type ambiguities, we must always specify the exact data type for constructor, via type attribute.
applicationContext.xml fooBean:
<bean id="fooBean" class="findnerd.snippets.enterprise.services.Foo">
<constructor-arg type="java.lang.String">
<value>fooname</value>
</constructor-arg>
<constructor-arg type="java.lang.String">
<value>100</value>
</constructor-arg>
<constructor-arg type="int">
<value>25</value>
</constructor-arg>
</bean>
Step:2.3 Run the application
Through the ApplicationContext the beans are loaded to App.class.
App.java:
package findnerd.snippets.enterprise;
/**
* Programmer : Bhagwan Singh
*/
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import findnerd.snippets.enterprise.services.HelloWorld;
public class App {
@SuppressWarnings("resource")
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorldBean");
System.out.println(helloWorld);
}
}
Step: 3. Setter-based dependency injection
Setter-based DI is accomplished by the container calling setter methods on the beans.
Step: 3.1 Create a simple Spring bean
We create a simple Spring bean, Bar and add a dependency to Foo. In this case, the Foo bean is injected via the setter method.
Bar.java:
package findnerd.snippets.enterprise.services;
/**
* Programmer : Bhagwan Singh
*/
public class Bar {
private Foo foo;
public void setFoo(Foo foo){
this.foo = foo;
}
public String toString(){
return "Bar! Foo : \n" + foo;
}
}
Step: 3.2 create applicationContext.xml and the bean definition must be added.
applicationContext.xml barBean:
<bean id="barBean" class="findnerd.snippets.enterprise.services.Bar">
<property name="foo">
<ref bean="fooBean" />
</property>
</bean>
Another way to accomplish the Dependency Injection via the setter is using the @Autowired annotation. Thus, we can get rid of the element in applicationContext.xml. The annotation is added to the setter of the injected bean. When Spring finds an @Autowired annotation used with setter methods, it tries to perform byType autowiring on the method.
Bar.class:
package findnerd.snippets.enterprise.services;
/**
* Programmer : Bhagwan Singh
*/
import org.springframework.beans.factory.annotation.Autowired;
public class Bar {
private Foo foo;
@Autowired
public void setFoo(Foo foo){
this.foo = foo;
}
public String toString(){
return "Bar! Foo : \n" + foo;
}
}
In applicationContext.xml attribute is defined. It's look for annotations on beans in the same application context it's defined in.
Create applicationContext.xml and put below code:
<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">
<context:annotation-config/>
<bean id="helloWorldBean"
class="findnerd.snippets.enterprise.services.HelloWorld">
<constructor-arg ref="fooBean" />
</bean>
<bean id="fooBean" class="findnerd.snippets.enterprise.services.Foo">
<constructor-arg type="java.lang.String">
<value>fooname</value>
</constructor-arg>
<constructor-arg type="java.lang.String">
<value>100</value>
</constructor-arg>
<constructor-arg type="int">
<value>25</value>
</constructor-arg>
</bean>
<bean id="barBean" class="findnerd.snippets.enterprise.services.Bar">
</bean>
</beans>
Step: 3.3 Run the application
App2.class:
package findnerd.snippets.enterprise;
/**
* Programmer : Bhagwan Singh
*/
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import findnerd.snippets.enterprise.services.Bar;
public class App2 {
@SuppressWarnings("resource")
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Bar bar = (Bar) context.getBean("barBean");
System.out.println(bar);
}
}
0 Comment(s)