SpringBoot中@Conditional注解的介紹及實(shí)踐
1、簡述
在 Spring Boot 中,@Conditional 注解用于實(shí)現(xiàn) 條件化 Bean 裝配,即根據(jù)特定的條件來決定是否加載某個(gè) Bean。它是 Spring 框架中的一個(gè)擴(kuò)展機(jī)制,常用于實(shí)現(xiàn)模塊化、可配置的組件加載。
本文將詳細(xì)介紹 @Conditional 相關(guān)的注解,包括 @ConditionalOnClass、@ConditionalOnMissingBean、@ConditionalOnProperty 等,并結(jié)合實(shí)際應(yīng)用示例講解其使用方式。
2、@Conditional 注解概述
@Conditional 是 Spring 4 引入的條件裝配注解,它可以根據(jù)外部環(huán)境或配置的狀態(tài),決定是否創(chuàng)建 Bean。
其核心接口是:
public interface Condition {
boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata);
}
任何實(shí)現(xiàn) Condition 接口的類,都可以用于自定義條件判斷。
@Configuration
public class MyConfig {
@Bean
@Conditional(MyCondition.class)
public MyService myService() {
return new MyService();
}
}
其中 MyCondition.class 需要實(shí)現(xiàn) Condition 接口,并提供判斷邏輯。
3、Spring Boot 內(nèi)置 @Conditional 相關(guān)注解
Spring Boot 提供了一些常見的 @Conditional 注解,簡化了條件判斷的邏輯:
| 注解 | 作用 |
|---|---|
@ConditionalOnClass | 類路徑下存在某個(gè)類時(shí),才加載該 Bean |
@ConditionalOnMissingClass | 類路徑下不存在某個(gè)類時(shí),才加載該 Bean |
@ConditionalOnBean | 當(dāng)容器中存在指定 Bean 時(shí),才加載當(dāng)前 Bean |
@ConditionalOnMissingBean | 當(dāng)容器中不存在指定 Bean 時(shí),才加載當(dāng)前 Bean |
@ConditionalOnProperty | 當(dāng)指定的配置屬性滿足條件時(shí),才加載當(dāng)前 Bean |
@ConditionalOnExpression | 當(dāng)指定的 SpEL 表達(dá)式為 true 時(shí),才加載當(dāng)前 Bean |
@ConditionalOnJava | 當(dāng) Java 版本符合要求時(shí),才加載當(dāng)前 Bean |
@ConditionalOnWebApplication | 當(dāng)應(yīng)用是 Web 應(yīng)用時(shí),才加載當(dāng)前 Bean |
@ConditionalOnNotWebApplication | 當(dāng)應(yīng)用不是 Web 應(yīng)用時(shí),才加載當(dāng)前 Bean |
3.1 @ConditionalOnClass 使用示例(類路徑檢測(cè))
我們希望在 Spring Boot 項(xiàng)目中,當(dāng)類路徑下存在 com.example.MyLibrary 這個(gè)類時(shí),才注冊(cè) MyService 這個(gè) Bean。
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyAutoConfiguration {
@Bean
@ConditionalOnClass(name = "com.example.MyLibrary")
public MyService myService() {
return new MyService();
}
}
解釋:
- 如果 com.example.MyLibrary 存在,則 MyService 會(huì)被創(chuàng)建。
- 如果 com.example.MyLibrary 不存在,則 MyService 不會(huì)被加載。
3.2 @ConditionalOnMissingBean(Bean 缺失時(shí)加載)
如果用戶沒有手動(dòng)定義 MyService,則提供一個(gè)默認(rèn)實(shí)現(xiàn)。
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public MyService myService() {
return new MyService("Default Implementation");
}
}
解釋:
- 如果 Spring 容器中已經(jīng)存在 MyService,則不會(huì)創(chuàng)建新的 Bean。
- 只有當(dāng) MyService 不存在時(shí),才會(huì)注冊(cè)默認(rèn)實(shí)現(xiàn)。
3.3 @ConditionalOnProperty(基于配置項(xiàng)條件加載 Bean)
我們希望 MyService 只有在 app.feature.enabled=true 時(shí)才被創(chuàng)建。
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyAutoConfiguration {
@Bean
@ConditionalOnProperty(name = "app.feature.enabled", havingValue = "true", matchIfMissing = false)
public MyService myService() {
return new MyService();
}
}
解釋:
- 如果 application.properties 中包含 app.feature.enabled=true,則 MyService 會(huì)被創(chuàng)建。
- 如果 app.feature.enabled=false,或者該屬性未定義,則 MyService 不會(huì)被加載。
在 application.properties 中啟用:
app.feature.enabled=true
3.4 @ConditionalOnBean(存在特定 Bean 時(shí)才加載)
當(dāng) UserService 存在時(shí),才創(chuàng)建 OrderService。
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class OrderServiceConfiguration {
@Bean
@ConditionalOnBean(UserService.class)
public OrderService orderService() {
return new OrderService();
}
}
解釋:
- 如果 UserService 存在,則 OrderService 也會(huì)被創(chuàng)建。
- 如果 UserService 不存在,則 OrderService 不會(huì)被加載。
3.5 @ConditionalOnExpression(基于 SpEL 表達(dá)式加載)
當(dāng) server.port 大于 8080 時(shí),才創(chuàng)建 AdvancedService。
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AdvancedConfig {
@Bean
@ConditionalOnExpression("#{T(java.lang.Integer).parseInt('${server.port:8080}') > 8080}")
public AdvancedService advancedService() {
return new AdvancedService();
}
}
解釋:
server.port > 8080 時(shí),AdvancedService 會(huì)被加載。
其他情況下,不會(huì)創(chuàng)建該 Bean。
在 application.properties 中配置:
server.port=9090
3.6 結(jié)合 @ConditionalOnClass 實(shí)現(xiàn) Starter 組件的熱拔插
我們要?jiǎng)?chuàng)建一個(gè) Spring Boot Starter 組件,如果用戶的 classpath 下存在 RedisTemplate,則自動(dòng)加載 Redis 相關(guān)的 Bean。
步驟:
創(chuàng)建 Starter 組件
@Configuration
@ConditionalOnClass(RedisTemplate.class)
public class RedisAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
return template;
}
}
應(yīng)用使用 Starter
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
如果引入 Redis 依賴,RedisAutoConfiguration 會(huì)自動(dòng)生效。
如果不引入 Redis 依賴,則 RedisTemplate 不會(huì)被創(chuàng)建。
4、總結(jié)
- @Conditional 及其衍生注解使 Spring Boot 具備了自動(dòng)配置和熱拔插的能力。
- @ConditionalOnClass 可用于判斷某個(gè)類是否存在,常用于 Starter 組件的自動(dòng)裝配。
- @ConditionalOnProperty 適用于基于配置的條件加載,增強(qiáng)靈活性。
- @ConditionalOnBean 和 @ConditionalOnMissingBean 適用于組件依賴管理。
合理使用這些注解,可以構(gòu)建更加模塊化、靈活、可配置的 Spring Boot 應(yīng)用。
以上就是SpringBoot中@Conditional注解的介紹及實(shí)踐的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot @Conditional注解的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java?awt-對(duì)話框簡單實(shí)現(xiàn)方式
這篇文章主要介紹了Java?awt-對(duì)話框簡單實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
jmeter實(shí)現(xiàn)接口關(guān)聯(lián)的兩種方式(正則表達(dá)式提取器和json提取器)
Jmeter用于接口測(cè)試時(shí),后一個(gè)接口經(jīng)常需要用到前一次接口返回的結(jié)果,本文主要介紹了jmeter實(shí)現(xiàn)接口關(guān)聯(lián)的兩種方式,感興趣的小伙伴們可以參考一下2021-11-11
spring-boot-autoconfigure模塊用法詳解
autoconfigure就是自動(dòng)配置的意思,spring-boot通過spring-boot-autoconfigure體現(xiàn)了"約定優(yōu)于配置"這一設(shè)計(jì)原則,而spring-boot-autoconfigure主要用到了spring.factories和幾個(gè)常用的注解條件來實(shí)現(xiàn)自動(dòng)配置,思路很清晰也很簡單,感興趣的朋友跟隨小編一起看看吧2022-11-11
利用Java+Selenium+OpenCV模擬實(shí)現(xiàn)網(wǎng)頁滑動(dòng)驗(yàn)證
目前很多網(wǎng)頁都有滑動(dòng)驗(yàn)證,目的就是防止不良爬蟲扒他們網(wǎng)站的數(shù)據(jù)。本文將介紹通過Java Selenium OpenCV解決網(wǎng)頁滑塊驗(yàn)證,需要的可以參考一下2022-01-01
SpringCloud Zuul網(wǎng)關(guān)功能實(shí)現(xiàn)解析
這篇文章主要介紹了SpringCloud Zuul網(wǎng)關(guān)功能實(shí)現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03

