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

SpringBoot?創(chuàng)建獲取yml里配置字段值

 更新時(shí)間:2025年06月19日 09:30:19   作者:柯南二號(hào)  
在Spring?Boot中通過@ConfigurationProperties綁定YML配置,創(chuàng)建Bean并提供訪問方法,實(shí)現(xiàn)根據(jù)配置字段動(dòng)態(tài)處理業(yè)務(wù)邏輯,具有一定的參考價(jià)值,感興趣的可以了解一下

比如在application.yml里有一個(gè)這樣的配置

proxy:
    enable: false

需要根據(jù)這個(gè)配置的不同來做不同的業(yè)務(wù)邏輯處理。

用@Component給這個(gè)類聲明是一個(gè)@Bean類,同時(shí)用@ConfigurationProperties這個(gè)來聲明前綴是yml里配置的“proxy”,然后聲明一個(gè)和yml配置"enable"同名的變量,配置上get和set方法。

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Setter
@Getter
@Component
@ConfigurationProperties(prefix = "proxy")
public class ProxySetting {
    private boolean enable;
}

創(chuàng)建一個(gè)這樣獲取全局Bean對(duì)象的工具類

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 獲取上下文的配置類
 */
@Component
public class SpringContextHolder implements ApplicationContextAware {
    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        SpringContextHolder.context = applicationContext;
    }

    public static <T> T getBean(Class<T> requiredType) {
        return context.getBean(requiredType);
    }
}

在需要用到這個(gè)yml里配置enable的地方,來做邏輯處理

ProxySetting proxySetting = SpringContextHolder.getBean(ProxySetting.class);
if (proxySetting.isEnable()) {
    // 處理enable為true的時(shí)候?qū)?yīng)的邏輯
} else {
   // 處理enable為false的時(shí)候?qū)?yīng)的邏輯
}

到此這篇關(guān)于SpringBoot 創(chuàng)建獲取yml里配置字段值的文章就介紹到這了,更多相關(guān)SpringBoot獲取yml配置字段值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

最新評(píng)論

兴和县| 宝山区| 黔江区| 新沂市| 上思县| 上饶市| 霞浦县| 凤山县| 桃园市| 上虞市| 金山区| 巴彦淖尔市| 榆林市| 吉木萨尔县| 大竹县| 宜章县| 会东县| 惠州市| 志丹县| 伊川县| 高要市| 会昌县| 肃北| 宁城县| 博野县| 筠连县| 仙居县| 卓资县| 仁怀市| 岚皋县| 开化县| 大姚县| 阿拉善左旗| 新闻| 长子县| 刚察县| 云安县| 定南县| 景宁| 罗源县| 太和县|