• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Autowired Annotation

June 8, 2015 By j2eereference Leave a Comment

Autowired Annotation

Spring provides support for annotation based container configurations.  On of them is @Autowired annotation .In one of our earlier posts, we had talked about Bean Auto-wiring : ByName, ByType and ByConstructor ( http://209.97.166.197/2015/06/bean-autowiring/ ). This post is about wiring the dependencies using Annotations, which we do by using @Autowired annotation.

In order to wire any dependency, we specify the the @Autowired into the member variable. This is actually auto-wiring by type which we have seen in our earlier posts which is sometimes ambiguous, depends on what types of beans we have specified in the spring.xml. However, @Autowired has a few other tricks that can make it less ambiguous.

Lets look at the example.

Bank.java is an interface

1
2
3
4
5
6
7
package com.j2eereference.spring;
 
public interface Bank {
public void getBranchInfo();
 
}

Branch.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.j2eereference.spring;
 
public class Branch {
private int code;
private String address;
public Branch(int code, String address) {
this.code = code;
this.address = address;
}
 
public int getCode() {
return code;
}
 
public void setCode(int code) {
this.code = code;
}
 
public String getAddress() {
return address;
}
 
public void setAddress(String address) {
this.address = address;
}
 
}

HDFC.java is the class where we wire the dependency for the member variable of type ‘Branch’

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.j2eereference.spring;
 
import org.springframework.beans.factory.annotation.Autowired;
 
public class HDFC implements Bank{
private Branch branch;
 
 
public Branch getBranch() {
return branch;
}
@Autowired
public void setBranch(Branch branch) {
this.branch = branch;
}
 
@Override
public void getBranchInfo()
{
System.out.println("HDFC Branch code : "+branch.getCode()+"and Address : "+branch.getAddress() );
}
}

Note that we have made ‘branch’ member variable of the bean as auto-wired.

spring.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8"?>  
 
<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"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
<bean id="bank" class="com.j2eereference.spring.HDFC" >
</bean>
<bean id="branchA" class="com.j2eereference.spring.Branch" >
<property name="code" value="1234"></property>
<property name="address" value="Bangalore"></property>
</bean>
<bean class="org.springframework.beans.factoty.annotation.AutowiredAnnotationBeanPostProcessor"></bean>
</beans>

@Autowired is a BeanPostPrecessor and hence we have to declare it in the spring.xml.

Now we have ‘hdfc’ bean and a ‘branchA’ bean.We have mentioned ‘branch’ member variable as auto-wired and we have supplied a bean ‘branchA’ whose type matches the type of the auto-wired member variable. It is very straightforward for Spring to know that it should auto-wire ‘branch’ member variable with ‘branchA’ bean.

It produces the below output:

HDFC Branch code : 1234 and Address : Bangalore

Now what happens if I declare multiple beans of type ‘Branch’?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="UTF-8"?>  
 
<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"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
<bean id="bank" class="com.j2eereference.spring.HDFC" >
</bean>
<bean id="branchA" class="com.j2eereference.spring.Branch" >
<property name="code" value="1234"></property>
<property name="address" value="Bangalore"></property>
</bean>
<bean id="branchB" class="com.j2eereference.spring.Branch" >
<property name="code" value="1234"></property>
<property name="address" value="Bangalore"></property>
</bean>
<bean id="branchC" class="com.j2eereference.spring.Branch" >
<property name="code" value="1234"></property>
<property name="address" value="Bangalore"></property>
</bean>
<bean class="org.springframework.beans.factoty.annotation.AutowiredAnnotationBeanPostProcessor"></bean>
</beans>

Note that we added two more beans ‘branchB’ and ‘branchC’ of type ‘Branch’. Now there three beans of type ‘Branch’. When we run this, we get an exception saying ‘Error creating bean with name ‘bank’: Injection of auto-wired dependencies failed. It means that the Spring is not able to find which bean it needs to be injected.

What we can do here is change the name of one of the beans to be same the the name the member variable. For example, the name of member variable is ‘branch’,so change any one of the bean names to be ‘branch’ and run it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="UTF-8"?>  
 
<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"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
<bean id="bank" class="com.j2eereference.spring.HDFC" >
</bean>
<bean id="branch" class="com.j2eereference.spring.Branch" >
<property name="code" value="1234"></property>
<property name="address" value="Bangalore"></property>
</bean>
<bean id="branchB" class="com.j2eereference.spring.Branch" >
<property name="code" value="1234"></property>
<property name="address" value="Bangalore"></property>
</bean>
<bean id="branchC" class="com.j2eereference.spring.Branch" >
<property name="code" value="1234"></property>
<property name="address" value="Bangalore"></property>
</bean>
<bean class="org.springframework.beans.factoty.annotation.AutowiredAnnotationBeanPostProcessor"></bean>
</beans>

Now it would produce the same output as before.

What happens here is, @Autowired annotation first looks for the type. If it finds only one bean of the required type, even though the name is different, it will auto-wire.

If there are multiple beans of the required type, what @Autowired annotation does is, as a second step, it will look for the name. If there a bean with same name as that of member variable, it will auto-wire it.

Now suppose that I cannot change the name of the bean, may be because it is referred in some other beans. The other option is to use @Qualifier. Now look at the below code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.j2eereference.spring;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
 
public class HDFC implements Bank{
private Branch branch;
 
 
public Branch getBranch() {
return branch;
}
@Autowired
@Qualifier("hdfcRelated")
public void setBranch(Branch branch) {
this.branch = branch;
}
 
@Override
public void getBranchInfo()
{
System.out.println("HDFC Branch code : "+branch.getCode()+"and Address : "+branch.getAddress() );
}
}

spring.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="UTF-8"?>  
 
<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"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
<bean id="bank" class="com.j2eereference.spring.HDFC" >
</bean>
<bean id="branchA" class="com.j2eereference.spring.Branch" >
<qualifier value="hdfcRelated"></qualifier>
<property name="code" value="1234"></property>
<property name="address" value="Bangalore"></property>
</bean>
<bean id="branchB" class="com.j2eereference.spring.Branch" >
<property name="code" value="1234"></property>
<property name="address" value="Bangalore"></property>
</bean>
<bean id="branchC" class="com.j2eereference.spring.Branch" >
<property name="code" value="1234"></property>
<property name="address" value="Bangalore"></property>
</bean>
<bean class="org.springframework.beans.factoty.annotation.AutowiredAnnotationBeanPostProcessor"></bean>
</beans>

Now run the application with these changes. It would produce the same output as before.

What happens here is, Spring will check for @Autowired. It will look at the beans for the matching type. Since there multiple beans of the same type, it will look for the matching name. But there is no such bean in this case. Next it will look for the Qualifier. If any of the beans have the qualifier that matches with the qualifier of the member variable to be auto-wired. It finds that there is only bean which matches the qualifier ‘hdfcRelated’ and wires the dependency.

Related Posts

  • Spring dependency injection with Java configuration
  • What is Spring Boot?
  • @import annotation in Spring
  • Configuring Spring Lifecycle Callbacks with java
  • Configuring Spring Bean Scope with java
  • Spring configuration using java
  • @Required annotation in spring
  • Event handling in Spring
  • Using MessageSource in Spring
  • Component annotation in spring

Filed Under: Spring

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.