Jan
16
2015

Dependency Injection in Spring framework

Hi in last Post of spring framework i explained about what is Dependency Injection (DI). In this post we will have a closer look of spring framework. In this post we will see what are the What is Actual meaning of Dependency injection, possible ways of Dependency injection , What are benefit of dependency injection .

What is Actual meaning of Dependency injection


Before we begin with what is dependency inject first we should  know what is dependency.Lets see it with a scenario. Let we have a Employee class that has field emp_add of type Address like this

Dependency Injection in spring

Here Employee class field emp_add type is Address. There is a dependency between Employee and Address. Employee class is dependent on Address class, So Employee is called dependent Class and Address is called dependency.

Now we can understand what is dependency injection meaning. Dependency injection is process of providing the dependencies (Address) inside a dependent(Employer) bean. Here in above scenario we are injecting Address in Employer bean. Dependency is any other object(address) the current object(emplyer) needs.

There are basically two ways of dependency injection.

  1. setter method injection
  2. constructor

In this post we will see setter method  and constructor injection.In next post we will see the difference between two.

Setter or property injection


In this example we will inject address object in employer object.Also we have injected primitive other dependency in object employer and address object.

Employer.java

Address.java

springbean.xml

In springbean.xml we have declare Employer.java and Address.java as bean using bean tag.To inject dependency we are using property sub-tag of bean.

DependencyInjection.java

OutPut

EMP_PIET
las vegas
India
california
lamp street

Collection injection using setter method


In this example we will see how we can inject collection inside bean object.Let take scenario where we are using employer object and we need to find all the addresses where the employer has lived so far. So we can inject list of addresses in employer object.

Employer.java

 Address.java

springbean.xml

To inject collection we make use of <list> tag which is a sub-tag of bean.In list tag we are using <ref>tag because we are injecting object. If we were to inject primitive type than we can use  <value> tag like this <value >your value goes here</value>.

CollectionInjection.java

OutPut:

Employer Name :Devender Kumar
Address :0
City :Gurgaon
Street :sitla mata road
State :haryana
Country :India
Address :1
City :jaipur
Street :housing board street
State :rajasthan
Country :india
Address :2
City :Chandigarh
Street :sec-22 A
State :UT
Country :India

 Constructor Injection


Instead of using setters method we will make use of parameterized  constructor to inject dependency.First we will look how to inject primitive type and single object then next we will look how to inject collection using constructor injection.

Employer.java

Address.java

springbean.java

To inject dependency we make use of <constructor-arg> sub-tag of <bean>.In <constructor-arg> tag we can specify name of of property to assign particular value to that property.

ConstructorInjection.java

Output

Name :devender kumar
Employee Address
Street :HNO 748 :sec- 22 A
City :Chandigarh
State :UT
Country :India

Collection injection using constructor


 

Employer.java

Address.java

springbean.xml

As we have seen in setter injection, we make use of <list> type to inject collection.If it were map then we could have use <map> tag . <list> is sub-tag of <constructor-arg>.

ConstructureInjectionPrimitiveType.java

output:

Name :devender
Address 0
Street : HNO748 sec 22 A
City :chandigarh
State :UT
Country :India
Address 1
Street : Mata Road
City :Gurgaon
State :Haryana
Country :India
Address 2
Street : Housing board
City :jaipur
State :Raj
Country :India

This is all in this post. In next post we will see difference between setter or property inject and constructor and which one is batter.

About the Author: devender kumar

java/j2EE developer

Comments are closed.