parent
57c10a1fac
commit
4ac521607e
|
|
@ -1,5 +1,5 @@
|
||||||
[[beans-autowired-annotation-primary]]
|
[[beans-autowired-annotation-primary]]
|
||||||
= Fine-tuning Annotation-based Autowiring with `@Primary`
|
= Fine-tuning Annotation-based Autowiring with `@Primary` or `@Fallback`
|
||||||
|
|
||||||
Because autowiring by type may lead to multiple candidates, it is often necessary to have
|
Because autowiring by type may lead to multiple candidates, it is often necessary to have
|
||||||
more control over the selection process. One way to accomplish this is with Spring's
|
more control over the selection process. One way to accomplish this is with Spring's
|
||||||
|
|
@ -50,8 +50,51 @@ Kotlin::
|
||||||
----
|
----
|
||||||
======
|
======
|
||||||
|
|
||||||
With the preceding configuration, the following `MovieRecommender` is autowired with the
|
Alternatively, as of 6.2, there is a `@Fallback` annotation for demarcating
|
||||||
`firstMovieCatalog`:
|
any beans other than the regular ones to be injected. If only one regular
|
||||||
|
bean is left, it is effectively primary as well:
|
||||||
|
|
||||||
|
[tabs]
|
||||||
|
======
|
||||||
|
Java::
|
||||||
|
+
|
||||||
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
||||||
|
----
|
||||||
|
@Configuration
|
||||||
|
public class MovieConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public MovieCatalog firstMovieCatalog() { ... }
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@Fallback
|
||||||
|
public MovieCatalog secondMovieCatalog() { ... }
|
||||||
|
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
----
|
||||||
|
|
||||||
|
Kotlin::
|
||||||
|
+
|
||||||
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
||||||
|
----
|
||||||
|
@Configuration
|
||||||
|
class MovieConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
fun firstMovieCatalog(): MovieCatalog { ... }
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@Fallback
|
||||||
|
fun secondMovieCatalog(): MovieCatalog { ... }
|
||||||
|
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
----
|
||||||
|
======
|
||||||
|
|
||||||
|
With both variants of the preceding configuration, the following
|
||||||
|
`MovieRecommender` is autowired with the `firstMovieCatalog`:
|
||||||
|
|
||||||
[tabs]
|
[tabs]
|
||||||
======
|
======
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,13 @@
|
||||||
[[beans-autowired-annotation-qualifiers]]
|
[[beans-autowired-annotation-qualifiers]]
|
||||||
= Fine-tuning Annotation-based Autowiring with Qualifiers
|
= Fine-tuning Annotation-based Autowiring with Qualifiers
|
||||||
|
|
||||||
`@Primary` is an effective way to use autowiring by type with several instances when one
|
`@Primary` and `@Fallback` are effective ways to use autowiring by type with several
|
||||||
primary candidate can be determined. When you need more control over the selection process,
|
instances when one primary (or non-fallback) candidate can be determined.
|
||||||
you can use Spring's `@Qualifier` annotation. You can associate qualifier values
|
|
||||||
with specific arguments, narrowing the set of type matches so that a specific bean is
|
When you need more control over the selection process, you can use Spring's `@Qualifier`
|
||||||
chosen for each argument. In the simplest case, this can be a plain descriptive value, as
|
annotation. You can associate qualifier values with specific arguments, narrowing the set
|
||||||
shown in the following example:
|
of type matches so that a specific bean is chosen for each argument. In the simplest case,
|
||||||
|
this can be a plain descriptive value, as shown in the following example:
|
||||||
|
|
||||||
--
|
--
|
||||||
[tabs]
|
[tabs]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue