Sunday 26 August 2012

Spring Autowiring Qualifier Example


Asynchronous Servlet Example                                                           Heap Generation In java


As we know spring beans are singleton by default, but if we need a two or more than two instance then what should we do to achieve this? In context of this solution spring provides a qualifier annotation to create and use two or more than two instances of a bean.


What is Qualifier annotaion
Since autowiring by type may lead to multiple candidates, it is often necessary to have more control over the selection process. One way to accomplish this is with Spring's @Qualifier annotation. This allows for associating qualifier values with specific arguments, narrowing the set of type matches so that a specific bean is chosen for each argument.

How to Use it?

SpringBeans.xml



<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
   <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
<task:annotation-driven />
 <bean id="emp" class="com.java.Employee" />
 <bean id="engineer1" class="com.java.Engineer" >
  <property name="name" value="agarwalishant1" />
 </bean>
 <bean id="engineer2" class="com.java. Engineer" >
  <property name="name" value="agarwalishant2" />
 </bean>
</beans>



Employee class 
package com.java;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class Employee {
@Autowired
@Qualifier("engineer1")
private Engineer  engineer1;
public Engineer getEngineer1() {
return engineer1;
}

public void setEngineer1( Engineer  engineer1) {
this.engineer1 = engineer1;
}
@Autowired
@Qualifier("engineer2")
private Engineer  engineer2;
public Engineer getEngineer2() {
return engineer2;
}

public void setEngineer2( Engineer  engineer2) {
this.engineer2 = engineer2;
}
@Override
public String toString() {
return "Employee [Engineer1=" + engineer1.name + ", Engineer2=" + engineer2.name + "]" ;

}
}

Engineer Class
package com.java;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

class Engineer
{
private String name ;
public void setName(String name){
this.name=name ;
}
public String getName(){
return name ;
}
}


Main class

package com.java;

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

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

Employee emp = (Employee) context.getBean("emp");
System.out.println(emp);
}
}



So in this example you can see the two instances of the Engineer class but named with different qualifier and
 Employee class will use it and display the desired output.

1 comment:

  1. Hi Ishant,

    I came across your page via facebook. Seems like you have just started writting about Java related stufff. We have a website where people like you share knowledge with others by writing articles(related to Java). Please let me know if you are interested in being editor of our website.

    Please visit our website:
    javabeginnerstutorial.com

    Ans let us know your views on the same.

    ReplyDelete