使用 @Primary
或 @Fallback
微調基於註解的自動裝配
由於依類型自動裝配可能會導致多個候選者,因此通常需要更精確地控制選擇過程。一種方法是使用 Spring 的 @Primary
註解。@Primary
表示當多個 Bean 成為自動裝配到單值相依性的候選者時,應優先考慮特定的 Bean。如果候選者中正好存在一個主要 Bean,則它將成為自動裝配的值。
考慮以下組態,其將 firstMovieCatalog
定義為主要的 MovieCatalog
-
Java
-
Kotlin
@Configuration
public class MovieConfiguration {
@Bean
@Primary
public MovieCatalog firstMovieCatalog() { ... }
@Bean
public MovieCatalog secondMovieCatalog() { ... }
// ...
}
@Configuration
class MovieConfiguration {
@Bean
@Primary
fun firstMovieCatalog(): MovieCatalog { ... }
@Bean
fun secondMovieCatalog(): MovieCatalog { ... }
// ...
}
或者,從 6.2 開始,有一個 @Fallback
註解,用於標記要注入的任何非常規 Bean。如果只剩下一個常規 Bean,它實際上也成為主要的 Bean
-
Java
-
Kotlin
@Configuration
public class MovieConfiguration {
@Bean
public MovieCatalog firstMovieCatalog() { ... }
@Bean
@Fallback
public MovieCatalog secondMovieCatalog() { ... }
// ...
}
@Configuration
class MovieConfiguration {
@Bean
fun firstMovieCatalog(): MovieCatalog { ... }
@Bean
@Fallback
fun secondMovieCatalog(): MovieCatalog { ... }
// ...
}
使用上述兩種組態變體,以下 MovieRecommender
都會自動裝配 firstMovieCatalog
-
Java
-
Kotlin
public class MovieRecommender {
@Autowired
private MovieCatalog movieCatalog;
// ...
}
class MovieRecommender {
@Autowired
private lateinit var movieCatalog: MovieCatalog
// ...
}
對應的 Bean 定義如下
<?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 class="example.SimpleMovieCatalog" primary="true">
<!-- inject any dependencies required by this bean -->
</bean>
<bean class="example.SimpleMovieCatalog">
<!-- inject any dependencies required by this bean -->
</bean>
<bean id="movieRecommender" class="example.MovieRecommender"/>
</beans>