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

SpringBoot中的@ConditionalOnBean注解用法解讀

 更新時(shí)間:2026年02月04日 09:12:47   作者:易xingxing  
本文介紹了SpringBoot中的@ConditionalOnBean條件注解,詳細(xì)講解了它的作用、基本用法、屬性、使用場(chǎng)景以及與@ConditionalOnMissingBean的區(qū)別,通過(guò)多個(gè)示例,幫助讀者更好地理解和應(yīng)用@ConditionalOnBean

1. 前言

在 Spring Boot 中,條件注解(Conditional 注解) 是一種強(qiáng)大的功能,允許我們根據(jù)某些條件動(dòng)態(tài)地注冊(cè)或跳過(guò)特定的 Bean。其中,@ConditionalOnBean 是最常用的條件注解之一,它的作用是:當(dāng) Spring 容器中存在指定的 Bean 時(shí),當(dāng)前 Bean 才會(huì)被注冊(cè)

本篇文章將詳細(xì)介紹 @ConditionalOnBean 的使用場(chǎng)景、原理,并提供多個(gè)示例幫助理解。

2.@ConditionalOnBean作用與基本用法

2.1@ConditionalOnBean的作用

@ConditionalOnBean 主要用于以下場(chǎng)景:

  • 按需加載 Bean:只有在某個(gè) Bean 存在時(shí),另一個(gè) Bean 才會(huì)被創(chuàng)建。
  • 模塊化設(shè)計(jì):某些功能模塊需要依賴(lài)特定 Bean 才能啟用,例如 僅當(dāng)某個(gè)組件存在時(shí),自動(dòng)配置才會(huì)生效。
  • 避免 Bean 沖突:如果某個(gè) Bean 依賴(lài)其他 Bean,則可使用 @ConditionalOnBean 確保它不會(huì)因缺少依賴(lài)而加載失敗。

2.2 基本用法

示例:當(dāng)DataSourceBean 存在時(shí),才創(chuàng)建MyServiceBean

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class MyConfig {

    @Bean
    public DataSource dataSource() {
        // 這里模擬 DataSource 實(shí)例,實(shí)際可用 HikariDataSource、Druid 等
        return new FakeDataSource();
    }

    @Bean
    @ConditionalOnBean(DataSource.class)  // 僅當(dāng) DataSource 存在時(shí),才創(chuàng)建 MyService
    public MyService myService() {
        return new MyService();
    }
}

class MyService {
    public MyService() {
        System.out.println("MyService 被創(chuàng)建");
    }
}

class FakeDataSource implements DataSource {
    // 這里可以模擬 DataSource 方法
}

執(zhí)行結(jié)果

MyService 被創(chuàng)建

如果 dataSource() 方法被注釋掉,則 MyService 不會(huì)被創(chuàng)建。

3.@ConditionalOnBean詳解

@ConditionalOnBean 提供了多個(gè)屬性,可以更加靈活地控制 Bean 的創(chuàng)建。

3.1value和type屬性(指定 Bean 類(lèi)型)

用于指定某種類(lèi)型的 Bean 存在時(shí),當(dāng)前 Bean 才會(huì)被注冊(cè)。

@Bean
@ConditionalOnBean(value = DataSource.class)  // 僅當(dāng) DataSource 存在時(shí)生效
public MyRepository myRepository() {
    return new MyRepository();
}

等效于:

@Bean
@ConditionalOnBean(type = "javax.sql.DataSource")  // 使用全限定類(lèi)名
public MyRepository myRepository() {
    return new MyRepository();
}

區(qū)別

  • value:直接使用 Class 類(lèi)型,編譯時(shí)檢查更安全。
  • type:使用字符串,可用于避免某些類(lèi)找不到(如可選依賴(lài))。

3.2name屬性(指定 Bean 名稱(chēng))

用于 指定某個(gè) Bean 名稱(chēng)是否存在 來(lái)決定當(dāng)前 Bean 是否加載。

@Bean
@ConditionalOnBean(name = "customBean")  // 僅當(dāng)名為 customBean 的 Bean 存在時(shí)注冊(cè)
public MyComponent myComponent() {
    return new MyComponent();
}

3.3annotation屬性(指定 Bean 需要標(biāo)注的注解)

可以指定某些 Bean 是否包含特定注解,如果包含,則當(dāng)前 Bean 才會(huì)被注冊(cè)。

@Bean
@ConditionalOnBean(annotation = Repository.class)  // 僅當(dāng)存在 @Repository 注解的 Bean 時(shí)生效
public MyService myService() {
    return new MyService();
}

3.4search屬性(搜索范圍)

默認(rèn)情況下,@ConditionalOnBean 只會(huì)在 當(dāng)前應(yīng)用上下文 中查找 Bean,而不會(huì)查找 父上下文(即 Spring Boot 的 ApplicationContext 層級(jí))。

search 選項(xiàng)可以指定搜索范圍:

  • ALL:在所有父子 ApplicationContext 中搜索。
  • CURRENT(默認(rèn)):僅搜索當(dāng)前 ApplicationContext。
@Bean
@ConditionalOnBean(value = DataSource.class, search = SearchStrategy.ALL) // 在所有上下文中搜索
public MyService myService() {
    return new MyService();
}

4.@ConditionalOnBean使用場(chǎng)景

場(chǎng)景 1:按需加載數(shù)據(jù)庫(kù)相關(guān) Bean

如果應(yīng)用程序中 使用了數(shù)據(jù)庫(kù),則提供一個(gè) DatabaseService,否則不創(chuàng)建:

@Bean
@ConditionalOnBean(DataSource.class)
public DatabaseService databaseService() {
    return new DatabaseService();
}

場(chǎng)景 2:?jiǎn)⒂媚承┳詣?dòng)配置

Spring Boot 的 spring-boot-autoconfigure 模塊大量使用 @ConditionalOnBean 來(lái)控制自動(dòng)配置。例如:

  • 只有當(dāng) DispatcherServlet 存在時(shí),Spring MVC 相關(guān)的自動(dòng)配置才會(huì)生效
@Configuration
@ConditionalOnBean(DispatcherServlet.class)
public class MvcAutoConfiguration {
    // 僅當(dāng) DispatcherServlet 存在時(shí),Spring MVC 配置生效
}

場(chǎng)景 3:可選依賴(lài)的組件

有時(shí),某些功能是可選的,比如當(dāng) Redis 組件存在時(shí),才創(chuàng)建緩存管理器:

@Bean
@ConditionalOnBean(name = "redisTemplate")  // 只有當(dāng) redisTemplate 存在時(shí)才加載
public CacheManager cacheManager() {
    return new RedisCacheManager();
}

5.@ConditionalOnBeanvs@ConditionalOnMissingBean

注解作用
@ConditionalOnBean當(dāng)指定 Bean 存在時(shí),才注冊(cè)當(dāng)前 Bean
@ConditionalOnMissingBean當(dāng)指定 Bean 不存在時(shí),才注冊(cè)當(dāng)前 Bean

示例:

@Bean
@ConditionalOnMissingBean(DataSource.class)  // 僅當(dāng) DataSource 不存在時(shí)才創(chuàng)建
public DataSource defaultDataSource() {
    return new DefaultDataSource();
}

6. 結(jié)論

在 Spring Boot 中,@ConditionalOnBean 可以幫助我們根據(jù) 是否存在特定 Bean 來(lái) 動(dòng)態(tài)注冊(cè) Bean,廣泛用于 按需加載、自動(dòng)配置 等場(chǎng)景。

小結(jié):

  • 指定 Bean 類(lèi)型@ConditionalOnBean(DataSource.class)
  • 指定 Bean 名稱(chēng)@ConditionalOnBean(name = "customBean")
  • 指定 Bean 注解@ConditionalOnBean(annotation = Repository.class)
  • 搜索范圍@ConditionalOnBean(search = SearchStrategy.ALL)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

凤阳县| 蚌埠市| 高平市| 剑河县| 平凉市| 历史| 疏勒县| 朝阳区| 聂拉木县| 宜兴市| 调兵山市| 西乡县| 阿拉善左旗| 思南县| 东明县| 扎鲁特旗| 青河县| 老河口市| 肇州县| 正宁县| 大港区| 申扎县| 普兰县| 丘北县| 北安市| 务川| 临潭县| 方山县| 车险| 九江县| 聊城市| 蓝山县| 永州市| 宁津县| 浦东新区| 南郑县| 普宁市| 宁城县| 凤山县| 晋中市| 咸阳市|