Shobhit Tyagi
Shobhit Tyagi

Reputation: 21

What is the alternative of using @Qualifier annotation in XML in Spring?

I have three classes in my spring project: class A, class B, and class C. Both A and B have a reference to C, and I want these references to refer to two different instances of C.

Now, I want to use auto-wiring byName. If I use Java-based configuration, then it can be achieved easily using @Autowired and @Qualifier annotations. But I want to achieve this using XML-based configuration.

In the code below, I want to inject beanC_A into beanA, and beanC_B into beanB using auto-wiring byName.

<!-- Definition for beans -->
<bean id = "beanA" class = "com.test.A" autowire="byName"/>

<bean id = "beanB" class = "com.test.B" autowire="byName"/>

<bean id = "beanC_A" class = "com.test.C"/>

<bean id = "beanC_B" class = "com.test.C"/>

I have already gone through What is the alternative of using Qualifier annotation in XML in Spring?

Upvotes: 1

Views: 1960

Answers (4)

dani-vta
dani-vta

Reputation: 6795

The XML alternative for the @Qualifier annotation is the <qualifier> element. However, a complete XML configuration is not possible, as the <qualifier> element can mark only bean definitions, not dependencies. Quoting the documentation of the <qualifier> element:

Bean definitions can provide qualifiers to match against annotations on a field or parameter for fine-grained autowire candidate resolution.

Bean definitions can provide qualifiers, i.e. annotation or XML, while fields and parameters can be qualified only via annotation. This is because an XML equivalent of @Qualifier for dependencies would be useless, since in XML you can refer to the exact dependency via ref attribute or <ref> element. For further details, check the section Fine-tuning Annotation-based Autowiring with Qualifiers of the Spring docs.

In your case, your XML and Java configuration should look the following way. Notice the usage of <context:annotation-config/> to allow the detection of annotations in the bean classes. The addition of this element is fundamental to include in the XML the configurations defined via @Qualifier on the arguments of A and B's constructors.

XML

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

    <bean id = "beanA" class = "com.test.A"/>
    <bean id = "beanB" class = "com.test.B"/>

    <bean id = "beanC_A" class = "com.test.C">
        <qualifier value="C_A"/>
    </bean>

    <bean id = "beanC_B" class = "com.test.C">
        <qualifier value="C_B"/>
    </bean>
</beans>

Beans

public class A {

    private C c;

    public A(@Qualifier("C_A") C c) {
        this.c = c;
    }

    // Rest of implementation ...
}

public class B {

    private C c;

    public B(@Qualifier("C_B") C c) {
        this.c = c;
    }

    // Rest of implementation ...
}

public class C {

    // Implementation...
}

Upvotes: 0

Michael Gantman
Michael Gantman

Reputation: 7792

First of all, XML configuration is anctient. Unless you have a special reason to stick with XML configuration I suggest you switch to annotations configuration. However, to your question, since you defined 2 different beans with class C - "beanC_A" and "beanC_B" the fact that they happen to be based on the same class is transparent to you. Your class A should inject bean "beanC_A" by id and your class B should inject bean "beanC_B". The @Qualifier annotation is relevant with annotations config where Spring tries to guess which bean to use based on class type. ANd in that case since you would have both in classes A and B a property of type C you would need to use a @Qualifier to tell exactly which bean out of two that you need. With XML config it is strait forward

Upvotes: 0

Edith
Edith

Reputation: 1

Make sure you have the annotation like below:

@Qualifier("QualifiedForA")
private A a;

Below is the XML configuration:

<context:annotation-config/>
<bean id = "beanA" class = "com.test.A"/>
    <qualifier value = "QualifiedForA" />
</bean>

Upvotes: 0

user7906470
user7906470

Reputation:

It is possible with the XML-based version of @Qualifier :

Short example:

<bean id="beanA" class="com.test.A1">
    <qualifier value = "AImpl1" />
</bean>

Please see the official documentation for more details: XML-Configuration Qualifier in Spring

Upvotes: 0

Related Questions