透過 @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,它實際上也成為了主要的 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>