There is an interface
public interface AddressInt { } Class that implements this interface
public class AddressA implements AddressInt { private String state; private String town; private String street; } The second class that implements this interface (it is the same, but it does not matter)
public class AddressB implements AddressInt { private String state; private String town; private String street; } A class that uses an interface type.
public class Employee { private String name; private String position; @Autowired @Qualifier("addressA") private AddressInt address; } Main method
public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("mySpringXMLConfig.xml"); Employee empl = (Employee)context.getBean("employee"); System.out.println(empl); } Configured so
<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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd "> <!--Register AutowiredAnnotationBeanPostProcessor--> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <!--<context:annotation-config/>--> <bean id="employee" class="by.Employee"> <property name="name" value="Nick Smith"/> <property name="position" value="Java developer"/> </bean> <bean id="addressA" class="by.AddressA"> <property name="state" value="NY"/> <property name="town" value="New York city"/> <property name="street" value="5"/> </bean> <bean id="addressB" class="by.AddressB"> <property name="state" value="IL"/> <property name="town" value="Chicago"/> <property name="street" value="37"/> </bean> </beans> Classics of the genre and everything is simple, but when executed
org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [by.AddressInt] is defined: expected single matching bean but found 2: [addressA, addressB] Googled For other people, I see the reasons for the refusal; The qualifier tried to set in different ways (and in xml, and to write the summary), but the result is one. What is the secret?
org.springframework.beans.factory.annotation.Qualifier. - talex