Spring auto-wiring using @Qualifier annotation example
Previous page
shows an example for auto-wiring using @Autowired annotation. What if a same bean declared twice? Which bean will be auto-wired?
Lets walk through the scenario:
package com.java2novice.beans;
public class Order {
private String item;
private String price;
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
|
In the given below xml configuration file, you can see two Order bean declarations:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<bean id="paymentGateway" class="com.java2novice.beans.PaymentGateway"/>
<bean id="javaOrderBean" class="com.java2novice.beans.Order" >
<property name="item" value="Java Book" />
<property name="price" value="RS 225" />
</bean>
<bean id="oracleOrderBean" class="com.java2novice.beans.Order" >
<property name="item" value="Oracle Book" />
<property name="price" value="RS 305" />
</bean>
</beans>
|
And in the below bean we are using @Autowired annotation, now which bean will be assigned? In this case an exception will be
thrown:
package com.java2novice.beans;
package com.java2novice.beans;
import org.springframework.beans.factory.annotation.Autowired;
public class PaymentGateway {
@Autowired
private Order order;
@Override
public String toString(){
return "ordering "+this.order.getItem()+" | price: "+this.order.getPrice();
}
}
|
Output: |
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'paymentGateway' defined in class path resource [applicationContext.xml]:
Unsatisfied dependency expressed through constructor argument with index 0 of type [com.java2novice.beans.Order]:
: No unique bean of type [com.java2novice.beans.Order] is defined: expected single matching bean but found 2:
[javaOrderBean, oracleOrderBean]; ]
|
To solve this issue, use @Qualifier annotation with bean name, so spring container picks the right bean declaration and
assigned to the property.
package com.java2novice.beans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class PaymentGateway {
@Autowired
@Qualifier("oracleOrderBean")
private Order order;
@Override
public String toString(){
return "ordering "+this.order.getItem()+" | price: "+this.order.getPrice();
}
}
|
Spring bean demo class:
package com.java2novice.test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.java2novice.beans.PaymentGateway;
public class SpringDemo {
public static void main(String a[]){
String confFile = "applicationContext.xml";
ConfigurableApplicationContext context
= new ClassPathXmlApplicationContext(confFile);
PaymentGateway paymentGateway = (PaymentGateway) context.getBean("paymentGateway");
System.out.println(paymentGateway.toString());
}
}
|
|