First, preface
This article undertakes the previous section: Spring_ Summary _04_ Advanced Configuration (ii) of the conditions annotated @conditional
When we assemble the bean in front, in the spring container, only one bean can match the desired result.
If more than one bean can match the result, spring will not be able to make a choice, which is the ambiguity of automatic assembly.
In this section we will address the ambiguity of automatic assembly
Ii. examples of ambiguity 1. Entity class
As follows, there are three classes that inherit from dessert
@Component Public class Implements Dessert {...} @Componentpublicclassimplements dessert {...} @Componentpublicclassimplements dessert {...}
2. Inject Beans
@Autowired Public void Setdessert (dessert dessert) { this. Dessert = dessert;}
3. Exceptions
Spring has selective difficulty, and when spring tries to automatically assemble the dessert parameter in Setdessert (), it finds three choices, and it throws a nouniquebeandefinitionexception exception to indicate that it has no choice.
@Primary and @Qualifier can be used to resolve ambiguity issues.
Third, @Primary
When declaring a bean, the ambiguity of automatic assembly can be avoided by setting one of the optional beans as the preferred (primary) bean.
Beans that are annotated by @primary can be preferred by the bean.
1. Configure the preferred bean
There are two ways to configure a preferred bean:
(1) Use in conjunction with @component in component classes
@Component @primary Public class Implements Dessert {...}
(2) Use with @bean in the configuration class
@Bean @primary Public Dessert icecream () { returnnew icecream ();}
2. Inject Beans
When the bean is injected, spring finds three optional beans, one of which is the preferred bean, so the preferred bean is chosen for injection.
@Autowired Public void Setdessert (dessert dessert) { this. Dessert = dessert;}
Note: Spring has a choice of difficulty, and if two or more of the preferred beans are configured, Spring has no choice.
Iv. @Qualifier
The Spring qualifier allows the bean to be qualified to the only bean that satisfies the requirement.
1. ID-based qualifiers
The parameter set for the @qualifier annotation is the ID of the bean you want to inject
@Autowired @qualifier ("icecream") // when injected, specifies that the injected bean is icecream Public void Setdessert (dessert dessert) { this. Dessert = dessert;}
2. Attribute-oriented Qualifiers
Instead of relying on the bean ID as a qualifier, we can set our own qualifiers for the bean.
You can use the @qualifier annotation to set qualifiers when declaring beans or configuring beans.
2.1 Setting Qualifiers
(1) When declaring a bean, set the qualifier
@Component @qualifier ("cold") // set qualifier to Cold Public class Implements Dessert {...}
(2) When configuring the bean, set the qualifier
@Bean @qualifier ("cold") // set qualifier to Cold Public Dessert icecream () { returnnew icecream ();}
2.2 Using Qualifiers
@Autowired @qualifier ("cold") // inject a bean with a qualifier of cold Public void Setdessert (dessert dessert) { this. Dessert = dessert;}
3 Custom Qualifier Annotations
Sometimes, we want to set multiple qualifiers for the Bean, as follows:
@Component @qualifier ("cold") // set qualifier to Cold@Qualifier ("creamy") // set Qualifier to creamypublicclass Implements dessert {...}
However,Java does not allow multiple annotations of the same type to recur on the same item
To solve this problem, we can create custom qualifier annotations.
(1) Self-defining qualifier annotations
@Cold
@Target ({ elementtype.type, elementtype.constructor, Elementtype.field, Elementtype.method }) @Retention (retentionpolicy.runtime) @Qualifier public @interface cold{}
@Creamy
@Target ({ elementtype.type, elementtype.constructor, Elementtype.field, Elementtype.method}) @Retention (retentionpolicy.runtime) @Qualifier public @Interface creamy{}
(2) Set multiple qualifiers for a bean
Public class Implements Dessert {...}
(3) When injecting beans, use multiple qualifiers to limit
@Autowired @cold@creamy Public void Setdessert (dessert dessert) { this. Dessert = dessert;}
Spring_ summarize the processing ambiguity of _04_ Advanced Configuration (iii)