最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot中@Conditional注解的介紹及實踐

 更新時間:2025年03月25日 08:41:50   作者:拾荒的小海螺  
在 Spring Boot 中,@Conditional 注解用于實現(xiàn) 條件化 Bean 裝配,本文將詳細介紹 @Conditional 相關(guān)的注解,并結(jié)合實際應(yīng)用示例講解其使用方式,感興趣的小伙伴可以了解下

1、簡述

在 Spring Boot 中,@Conditional 注解用于實現(xiàn) 條件化 Bean 裝配,即根據(jù)特定的條件來決定是否加載某個 Bean。它是 Spring 框架中的一個擴展機制,常用于實現(xiàn)模塊化、可配置的組件加載。

本文將詳細介紹 @Conditional 相關(guān)的注解,包括 @ConditionalOnClass、@ConditionalOnMissingBean、@ConditionalOnProperty 等,并結(jié)合實際應(yīng)用示例講解其使用方式。

2、@Conditional 注解概述

@Conditional 是 Spring 4 引入的條件裝配注解,它可以根據(jù)外部環(huán)境或配置的狀態(tài),決定是否創(chuàng)建 Bean。

其核心接口是:

public interface Condition {
    boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata);
}

任何實現(xiàn) Condition 接口的類,都可以用于自定義條件判斷。

@Configuration
public class MyConfig {

    @Bean
    @Conditional(MyCondition.class) 
    public MyService myService() {
        return new MyService();
    }
}

其中 MyCondition.class 需要實現(xiàn) Condition 接口,并提供判斷邏輯。

3、Spring Boot 內(nèi)置 @Conditional 相關(guān)注解

Spring Boot 提供了一些常見的 @Conditional 注解,簡化了條件判斷的邏輯:

注解作用
@ConditionalOnClass類路徑下存在某個類時,才加載該 Bean
@ConditionalOnMissingClass類路徑下不存在某個類時,才加載該 Bean
@ConditionalOnBean當(dāng)容器中存在指定 Bean 時,才加載當(dāng)前 Bean
@ConditionalOnMissingBean當(dāng)容器中不存在指定 Bean 時,才加載當(dāng)前 Bean
@ConditionalOnProperty當(dāng)指定的配置屬性滿足條件時,才加載當(dāng)前 Bean
@ConditionalOnExpression當(dāng)指定的 SpEL 表達式為 true 時,才加載當(dāng)前 Bean
@ConditionalOnJava當(dāng) Java 版本符合要求時,才加載當(dāng)前 Bean
@ConditionalOnWebApplication當(dāng)應(yīng)用是 Web 應(yīng)用時,才加載當(dāng)前 Bean
@ConditionalOnNotWebApplication當(dāng)應(yīng)用不是 Web 應(yīng)用時,才加載當(dāng)前 Bean

3.1 @ConditionalOnClass 使用示例(類路徑檢測)

我們希望在 Spring Boot 項目中,當(dāng)類路徑下存在 com.example.MyLibrary 這個類時,才注冊 MyService 這個 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 會被創(chuàng)建。
  • 如果 com.example.MyLibrary 不存在,則 MyService 不會被加載。

3.2 @ConditionalOnMissingBean(Bean 缺失時加載)

如果用戶沒有手動定義 MyService,則提供一個默認實現(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,則不會創(chuàng)建新的 Bean。
  • 只有當(dāng) MyService 不存在時,才會注冊默認實現(xiàn)。

3.3 @ConditionalOnProperty(基于配置項條件加載 Bean)

我們希望 MyService 只有在 app.feature.enabled=true 時才被創(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 會被創(chuàng)建。
  • 如果 app.feature.enabled=false,或者該屬性未定義,則 MyService 不會被加載。

在 application.properties 中啟用:

app.feature.enabled=true

3.4 @ConditionalOnBean(存在特定 Bean 時才加載)

當(dāng) UserService 存在時,才創(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 也會被創(chuàng)建。
  • 如果 UserService 不存在,則 OrderService 不會被加載。

3.5 @ConditionalOnExpression(基于 SpEL 表達式加載)

當(dāng) server.port 大于 8080 時,才創(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 時,AdvancedService 會被加載。

其他情況下,不會創(chuàng)建該 Bean。

在 application.properties 中配置:

server.port=9090

3.6 結(jié)合 @ConditionalOnClass 實現(xiàn) Starter 組件的熱拔插

我們要創(chuàng)建一個 Spring Boot Starter 組件,如果用戶的 classpath 下存在 RedisTemplate,則自動加載 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 會自動生效。

如果不引入 Redis 依賴,則 RedisTemplate 不會被創(chuàng)建。

4、總結(jié)

  • @Conditional 及其衍生注解使 Spring Boot 具備了自動配置和熱拔插的能力。
  • @ConditionalOnClass 可用于判斷某個類是否存在,常用于 Starter 組件的自動裝配。
  • @ConditionalOnProperty 適用于基于配置的條件加載,增強靈活性。
  • @ConditionalOnBean 和 @ConditionalOnMissingBean 適用于組件依賴管理。

合理使用這些注解,可以構(gòu)建更加模塊化、靈活、可配置的 Spring Boot 應(yīng)用。

以上就是SpringBoot中@Conditional注解的介紹及實踐的詳細內(nèi)容,更多關(guān)于SpringBoot @Conditional注解的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 淺析SpringBoot如何解決CORS問題

    淺析SpringBoot如何解決CORS問題

    在前后端分離的開發(fā)模式中,經(jīng)常會遇到 跨域資源共享(CORS) 的問題,本文將全面介紹 Spring Boot 中處理 CORS 的常見方法,大家可以根據(jù)需要進行選擇
    2025-05-05
  • SpringBoot中實現(xiàn)啟動任務(wù)的實現(xiàn)步驟

    SpringBoot中實現(xiàn)啟動任務(wù)的實現(xiàn)步驟

    這篇文章主要介紹了SpringBoot中實現(xiàn)啟動任務(wù)的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • java LRU(Least Recently Used )詳解及實例代碼

    java LRU(Least Recently Used )詳解及實例代碼

    這篇文章主要介紹了java LRU(Least Recently Used )詳解及實例代碼的相關(guān)資料,Java里面實現(xiàn)LRU緩存通常有兩種選擇,一種是使用LinkedHashMap,一種是自己設(shè)計數(shù)據(jù)結(jié)構(gòu),使用鏈表+HashMap,需要的朋友可以參考下
    2016-11-11
  • Java 重寫與重載方法與區(qū)別詳解

    Java 重寫與重載方法與區(qū)別詳解

    本篇文章通過實例詳細介紹了重寫與重載,以及他們的區(qū)別,需要的朋友可以參考下
    2017-04-04
  • spring-boot https證書雙向認證配置的實現(xiàn)

    spring-boot https證書雙向認證配置的實現(xiàn)

    本文詳細介紹SpringBoot項目中配置自簽名CA證書、簽發(fā)服務(wù)端和客戶端證書,生成PKCS12格式的證書,及設(shè)置SSL/TLS配置,實現(xiàn)雙向認證,具有一定的參考價值,感興趣的可以了解一下
    2025-08-08
  • Java實現(xiàn)stream的三個常用方式(toMap,groupingBy,findFirst)

    Java實現(xiàn)stream的三個常用方式(toMap,groupingBy,findFirst)

    本文主要介紹了Java實現(xiàn)stream的三個常用方式,主要包括toMap,groupingBy,findFirst,具有一定的參考價值,感興趣的可以了解一下
    2023-10-10
  • Java多線程Future松獲取異步任務(wù)結(jié)果輕松實現(xiàn)

    Java多線程Future松獲取異步任務(wù)結(jié)果輕松實現(xiàn)

    這篇文章主要為大家介紹了Java多線程Future松獲取異步任務(wù)結(jié)果輕松實現(xiàn)方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • 帶你用Java全面剖析類和對象

    帶你用Java全面剖析類和對象

    下面小編就為大家?guī)硪黄胬斫釰ava類和對象。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2021-09-09
  • 詳解Spring關(guān)于@Resource注入為null解決辦法

    詳解Spring關(guān)于@Resource注入為null解決辦法

    這篇文章主要介紹了詳解Spring關(guān)于@Resource注入為null解決辦法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Java多線程事務(wù)回滾@Transactional失效處理方案

    Java多線程事務(wù)回滾@Transactional失效處理方案

    這篇文章主要介紹了Java多線程事務(wù)回滾@Transactional失效處理方案,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下
    2022-08-08

最新評論

十堰市| 多伦县| 时尚| 八宿县| 宜州市| 洛南县| 德安县| 贵南县| 西充县| 库尔勒市| 苍南县| 阳春市| 临沂市| 黔江区| 盐边县| 息烽县| 屏东市| 乐昌市| 于都县| 湖北省| 凤山市| 八宿县| 东海县| 宁武县| 墨竹工卡县| 双柏县| 驻马店市| 阿城市| 五寨县| 龙川县| 新津县| 连平县| 宁武县| 宁远县| 富阳市| 平和县| 临漳县| 长寿区| 旬阳县| 上栗县| 富阳市|