Spring - Fine-tuning annotation-based autowiring with qualifiers
@Primary is an effective way to use autowiring by type with several instances when one primary
candidate can be determined. When more control over the selection process is required, Spring’s
@Qualifier annotation can be used. You can associate qualifier values with specific arguments,
narrowing the set of type matches so that a specific bean is chosen for each argument. In the simplest
case, this can be a plain descriptive value:
public class MovieRecommender {
@Autowired @Qualifier("main")
private MovieCatalog movieCatalog;
// ...
}
The @Qualifier annotation can also be specified on individual constructor arguments or method parameters:
public class MovieRecommender {
private MovieCatalog movieCatalog;
private CustomerPreferenceDao customerPreferenceDao;
@Autowired
public void prepare(@Qualifier("main")MovieCatalog movieCatalog, CustomerPreferenceDao customerPreferenceDao) {
this.movieCatalog = movieCatalog;
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}
The corresponding bean definitions appear as follows. The bean with qualifier value "main" is wired with the constructor argument that is qualified with the same value.
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean class="example.SimpleMovieCatalog"> <qualifier value="main"/> <!-- inject any dependencies required by this bean --> </bean> <bean class="example.SimpleMovieCatalog"> <qualifier value="action"/> <!-- inject any dependencies required by this bean --> </bean> <bean id="movieRecommender" class="example.MovieRecommender"/> </beans>
For a fallback match, the bean name is considered a default qualifier value. Thus you can define the bean with an id "main" instead of the nested qualifier element, leading to the same matching result. However, although you can use this convention to refer to specific beans by name, @Autowired is fundamentally about type-driven injection with optional semantic qualifiers. This means that qualifier values, even with the bean name fallback, always have narrowing semantics within the set of type matches; they do not semantically express a reference to a unique bean id. Good qualifier values are "main" or "EMEA" or "persistent", expressing characteristics of a specific component that are independent from the bean id, which may be auto-generated in case of an anonymous bean definition like the one in the preceding example.
Qualifiers also apply to typed collections, as discussed above, for example, to Set. In
this case, all matching beans according to the declared qualifiers are injected as a collection. This implies
that qualifiers do not have to be unique; they rather simply constitute filtering criteria. For example, you
can define multiple MovieCatalog beans with the same qualifier value "action", all of which would be
injected into a Set annotated with @Qualifier("action").
If you intend to express annotation-driven injection by name, do not primarily use @Autowired,
even if is technically capable of referring to a bean name through @Qualifier values. Instead,
use the JSR-250 @Resource annotation, which is semantically defined to identify a specific
target component by its unique name, with the declared type being irrelevant for the matching
process. @Autowired has rather different semantics: After selecting candidate beans by type,
the specified String qualifier value will be considered within those type-selected candidates only,
e.g. matching an "account" qualifier against beans marked with the same qualifier label.
For beans that are themselves defined as a collection/map or array type, @Resource is a fine
solution, referring to the specific collection or array bean by unique name. That said, as of 4.3,
collection/map and array types can be matched through Spring’s @Autowired type matching
algorithm as well, as long as the element type information is preserved in @Bean return type
signatures or collection inheritance hierarchies. In this case, qualifier values can be used to select
among same-typed collections, as outlined in the previous paragraph.
As of 4.3, @Autowired also considers self references for injection, i.e. references back to the
bean that is currently injected. Note that self injection is a fallback; regular dependencies on other
components always have precedence. In that sense, self references do not participate in regular
candidate selection and are therefore in particular never primary; on the contrary, they always end
up as lowest precedence. In practice, use self references as a last resort only, e.g. for calling other
methods on the same instance through the bean’s transactional proxy: Consider factoring out the
affected methods to a separate delegate bean in such a scenario. Alternatively, use @Resource
which may obtain a proxy back to the current bean by its unique name.
@Autowired applies to fields, constructors, and multi-argument methods, allowing for narrowing
through qualifier annotations at the parameter level. By contrast, @Resource is supported only
for fields and bean property setter methods with a single argument. As a consequence, stick with
qualifiers if your injection target is a constructor or a multi-argument method.
You can create your own custom qualifier annotations. Simply define an annotation and provide the @Qualifier annotation within your definition:
@Target({ElementType.FIELD, ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Genre {
String value();
}
public class MovieRecommender {
@Autowired @Qualifier("main")
private MovieCatalog movieCatalog;
// ...
}
The @Qualifier annotation can also be specified on individual constructor arguments or method parameters:
public class MovieRecommender {
private MovieCatalog movieCatalog;
private CustomerPreferenceDao customerPreferenceDao;
@Autowired
public void prepare(@Qualifier("main")MovieCatalog movieCatalog, CustomerPreferenceDao customerPreferenceDao) {
this.movieCatalog = movieCatalog;
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}
The corresponding bean definitions appear as follows. The bean with qualifier value "main" is wired with the constructor argument that is qualified with the same value.
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean class="example.SimpleMovieCatalog"> <qualifier value="main"/> <!-- inject any dependencies required by this bean --> </bean> <bean class="example.SimpleMovieCatalog"> <qualifier value="action"/> <!-- inject any dependencies required by this bean --> </bean> <bean id="movieRecommender" class="example.MovieRecommender"/> </beans>
For a fallback match, the bean name is considered a default qualifier value. Thus you can define the bean with an id "main" instead of the nested qualifier element, leading to the same matching result. However, although you can use this convention to refer to specific beans by name, @Autowired is fundamentally about type-driven injection with optional semantic qualifiers. This means that qualifier values, even with the bean name fallback, always have narrowing semantics within the set of type matches; they do not semantically express a reference to a unique bean id. Good qualifier values are "main" or "EMEA" or "persistent", expressing characteristics of a specific component that are independent from the bean id, which may be auto-generated in case of an anonymous bean definition like the one in the preceding example.
Qualifiers also apply to typed collections, as discussed above, for example, to Set
You can create your own custom qualifier annotations. Simply define an annotation and provide the @Qualifier annotation within your definition:
@Target({ElementType.FIELD, ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Genre {
String value();
}
Comments
Post a Comment