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

Spring示例講解條件注入方法

 更新時間:2022年06月21日 10:58:45   作者:IT利刃出鞘  
Spring支持按照條件來注入某些特定的bean,這也是Spring Boot實(shí)現(xiàn)自動化配置的底層方法,文中的示例代碼講解詳細(xì),需要的可以參考一下

簡介

說明

本文用實(shí)例介紹Spring的條件注入的用法。

@Component、@Configuration+@Bean都可以與條件注入的注解結(jié)合。

@Component+條件注解

Bean

package com.example.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
@Component
@ConditionalOnProperty(name = "custom.myComponent.enabled", havingValue = "true")
public class MyComponent {
    public MyComponent() {
        System.out.println("[MyComponent#MyComponent]");
    }
}

application.yml

custom:
  myComponent:
    enabled: true

運(yùn)行結(jié)果:

[MyComponent#MyComponent]

若將application.yml的custom.myComponent.enabled去掉,或者設(shè)置為非true值,則不會輸出上邊的運(yùn)行結(jié)果。

@Configuration+@Bean+條件注解

Bean

package com.example.config;
public class MyComponent {
    public MyComponent() {
        System.out.println("[MyComponent#MyComponent]");
    }
}

配置類

package com.example.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyConfig {
    @Bean
    @ConditionalOnProperty(name = "custom.myComponent.enabled", havingValue = "true")
    public MyComponent getMyComponent() {
        return new MyComponent();
    }
}

application.yml

custom:
  myComponent:
    enabled: true

運(yùn)行結(jié)果:

[MyComponent#MyComponent]

若將application.yml的custom.myComponent.enabled去掉,或者設(shè)置為非true值,則不會輸出上邊的運(yùn)行結(jié)果。

@Configuration+條件注解+@Bean

Bean

package com.example.config;
public class MyComponent {
    public MyComponent() {
        System.out.println("[MyComponent#MyComponent]");
    }
}

配置類

package com.example.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnProperty(name = "custom.myComponent.enabled", havingValue = "true")
public class MyConfig {
    @Bean
    public MyComponent getMyComponent() {
        return new MyComponent();
    }
}

application.yml

custom:
  myComponent:
    enabled: true

運(yùn)行結(jié)果:

[MyComponent#MyComponent]

若將application.yml的custom.myComponent.enabled去掉,或者設(shè)置為非true值,則不會輸出上邊的運(yùn)行結(jié)果。

自定義Condition

自定義的condition的matches方法返回值為true時,才會創(chuàng)建bean。

條件類

//判斷當(dāng)前系統(tǒng)是否是Mac

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class MyCondition implements Condition {
    @Override
    public boolean matches(ConditionContext conditionContext, 
                           AnnotatedTypeMetadata annotatedTypeMetadata) {
        return conditionContext.getEnvironment().getProperty("os.name").contains("Mac");
    }
}
@Configuration
public class Config {
    @Conditional(MyCondition.class)
    @Bean
    public String condition() {
        System.err.println("This is mac");
        return "";
    }
}

到此這篇關(guān)于Spring示例講解條件注入方法的文章就介紹到這了,更多相關(guān)Spring條件注入內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Kafka中消息隊列的兩種模式講解

    Kafka中消息隊列的兩種模式講解

    這篇文章主要介紹了Kafka中消息隊列的兩種模式講解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • Idea去除方法形參參數(shù)提示的操作

    Idea去除方法形參參數(shù)提示的操作

    這篇文章主要介紹了Idea去除方法形參參數(shù)提示的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • Spring Boot中單例類實(shí)現(xiàn)對象的注入方式

    Spring Boot中單例類實(shí)現(xiàn)對象的注入方式

    這篇文章主要介紹了Spring Boot中單例類實(shí)現(xiàn)對象的注入方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • SpringBoot全局處理統(tǒng)一返回類型方式

    SpringBoot全局處理統(tǒng)一返回類型方式

    這篇文章主要介紹了SpringBoot全局處理統(tǒng)一返回類型方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • 深入理解Java 線程通信

    深入理解Java 線程通信

    這篇文章主要介紹了Java 線程通信的的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • Java中Instant的使用及轉(zhuǎn)換

    Java中Instant的使用及轉(zhuǎn)換

    Instant是java.time包中的一個類,本文主要介紹了Java中Instant的使用及轉(zhuǎn)換,具有一定的參考價值,感興趣的可以了解一下
    2024-06-06
  • JavaEE中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析

    JavaEE中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析

    這篇文章主要為大家詳細(xì)介紹了JavaEE中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例,感興趣的小伙伴們可以參考一下
    2016-05-05
  • 一篇文章帶你入門Java之編程規(guī)范

    一篇文章帶你入門Java之編程規(guī)范

    這篇文章主要介紹了如何養(yǎng)成良好java代碼編碼規(guī)范,規(guī)范需要平時編碼過程中注意,是一個慢慢養(yǎng)成的好習(xí)慣,下面小編就帶大家來一起詳細(xì)了解一下吧
    2021-08-08
  • Java私有構(gòu)造函數(shù)作用原理解析

    Java私有構(gòu)造函數(shù)作用原理解析

    這篇文章主要介紹了Java私有構(gòu)造函數(shù)作用原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-12-12
  • Java結(jié)合EasyExcel構(gòu)建復(fù)雜多級表頭

    Java結(jié)合EasyExcel構(gòu)建復(fù)雜多級表頭

    在Java開發(fā)中,處理Excel文件時,構(gòu)建復(fù)雜的多級表頭是一項常見且具有挑戰(zhàn)性的任務(wù),下面小編就來和大家聊聊如何通過自定義方法實(shí)現(xiàn)多級表頭的構(gòu)建吧
    2025-03-03

最新評論

融水| 金乡县| 沙坪坝区| 龙泉市| 杭州市| 景谷| 繁峙县| 紫阳县| 虹口区| 浙江省| 郴州市| 闵行区| 贵溪市| 大名县| 获嘉县| 崇仁县| 茌平县| 招远市| 清河县| 南平市| 英超| 漳浦县| 且末县| 云龙县| 波密县| 九龙城区| 县级市| 荔浦县| 锡林郭勒盟| 凌源市| 富顺县| 札达县| 小金县| 孙吴县| 苍梧县| 渑池县| 原阳县| 松江区| 仁布县| 自贡市| 象山县|