|
1 | 1 | [[beans-autowired-annotation-primary]]
|
2 |
| -= Fine-tuning Annotation-based Autowiring with `@Primary` |
| 2 | += Fine-tuning Annotation-based Autowiring with `@Primary` or `@Fallback` |
3 | 3 |
|
4 | 4 | Because autowiring by type may lead to multiple candidates, it is often necessary to have
|
5 | 5 | more control over the selection process. One way to accomplish this is with Spring's
|
@@ -50,8 +50,51 @@ Kotlin::
|
50 | 50 | ----
|
51 | 51 | ======
|
52 | 52 |
|
53 |
| -With the preceding configuration, the following `MovieRecommender` is autowired with the |
54 |
| -`firstMovieCatalog`: |
| 53 | +Alternatively, as of 6.2, there is a `@Fallback` annotation for demarcating |
| 54 | +any beans other than the regular ones to be injected. If only one regular |
| 55 | +bean is left, it is effectively primary as well: |
| 56 | + |
| 57 | +[tabs] |
| 58 | +====== |
| 59 | +Java:: |
| 60 | ++ |
| 61 | +[source,java,indent=0,subs="verbatim,quotes",role="primary"] |
| 62 | +---- |
| 63 | + @Configuration |
| 64 | + public class MovieConfiguration { |
| 65 | +
|
| 66 | + @Bean |
| 67 | + public MovieCatalog firstMovieCatalog() { ... } |
| 68 | +
|
| 69 | + @Bean |
| 70 | + @Fallback |
| 71 | + public MovieCatalog secondMovieCatalog() { ... } |
| 72 | +
|
| 73 | + // ... |
| 74 | + } |
| 75 | +---- |
| 76 | +
|
| 77 | +Kotlin:: |
| 78 | ++ |
| 79 | +[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"] |
| 80 | +---- |
| 81 | + @Configuration |
| 82 | + class MovieConfiguration { |
| 83 | +
|
| 84 | + @Bean |
| 85 | + fun firstMovieCatalog(): MovieCatalog { ... } |
| 86 | +
|
| 87 | + @Bean |
| 88 | + @Fallback |
| 89 | + fun secondMovieCatalog(): MovieCatalog { ... } |
| 90 | +
|
| 91 | + // ... |
| 92 | + } |
| 93 | +---- |
| 94 | +====== |
| 95 | + |
| 96 | +With both variants of the preceding configuration, the following |
| 97 | +`MovieRecommender` is autowired with the `firstMovieCatalog`: |
55 | 98 |
|
56 | 99 | [tabs]
|
57 | 100 | ======
|
|
0 commit comments