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

SpringBoot中獲取profile的方法詳解

 更新時(shí)間:2022年04月08日 08:18:40   作者:m0_54850467  
這篇文章主要介紹了springboot獲取profile的操作,文中的示例代碼講解詳細(xì),具有很好的參考價(jià)值,希望對大家有所幫助

spring boot與profile

spring boot 的項(xiàng)目中不再使用xml的方式進(jìn)行配置,并且,它還遵循著約定大于配置。

靜態(tài)獲取方式

靜態(tài)工具類獲取當(dāng)前項(xiàng)目的profile環(huán)境。

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

import java.util.Locale;

/**
 * 

 */
/**
 * @author wangjiuzhou (835540436@qq.com)
 * @date 2018/10/27
 * 項(xiàng)目名稱:
 * 類名: SpringContextUtil
 * 描述: 獲取bean的工具類,可用于在線程里面獲取bean
 */
@Component
public class SpringContextUtil implements ApplicationContextAware {

    public static final String LOCAL_PROFILE = "local";
    public static final String DEV_PROFILE = "dev";
    public static final String TEST_PROFILE = "test";
    public static final String PRO_PROFILE = "pro";


    private static ApplicationContext context = null;


    /* (non Javadoc)
     * @Title: setApplicationContext
     * @Description: spring獲取bean工具類
     * @param applicationContext
     * @throws BeansException
     * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        context = applicationContext;
    }
 
    // 傳入線程中
    public static <T> T getBean(String beanName) {
        return (T) context.getBean(beanName);
    }
 
    // 國際化使用
    public static String getMessage(String key) {
        return context.getMessage(key, null, Locale.getDefault());
    }
 
    // 獲取當(dāng)前環(huán)境
    public static String getActiveProfile() {
        return context.getEnvironment().getActiveProfiles()[0];
    }
}

點(diǎn)評:

這種方式在使用起來很方便也是現(xiàn)在各個(gè)博客文章所撰寫的方式,在很多Service的業(yè)務(wù)代碼中使用起來很方便,畢竟是靜態(tài)的方式嘛!

但是有一種缺陷,因?yàn)閷?shí)現(xiàn)ApplicationContextAware接口,而spring中的這個(gè)接口是在所有的Bean注入完畢,才會(huì)執(zhí)行setApplicationContext方法,那么問題來了,往往在項(xiàng)目中我們可能會(huì)對一些Bean進(jìn)行一些config操作,例如:@Bean注入,而有時(shí)候我們會(huì)根據(jù)不同的profile進(jìn)行不同的定制化config。這個(gè)時(shí)候恰恰我們的工具類SpringContextUtil還沒有執(zhí)行setApplicationContext此時(shí)工具類中的context對象還是null。就會(huì)出現(xiàn)異常的情況。下面的方式可以彌補(bǔ)這個(gè)缺陷。

autowire ProfileConfig

使用這種方式首先聲明一下,其實(shí)就相當(dāng)于一個(gè)特殊的configBean一樣,因?yàn)橹挥羞@樣,這個(gè)類才不會(huì)在所有bean全部加載完畢后才能獲取到context。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;

/**
 * @author wangjiuzhou (835540436@qq.com)
 * @date 2018/11/07
 *
 * 獲取當(dāng)前項(xiàng)目環(huán)境:local、dev、test、pro
 */
@Configuration
public class ProfileConfig {
    public static final String LOCAL_PROFILE = "local";
    public static final String DEV_PROFILE = "dev";
    public static final String TEST_PROFILE = "test";
    public static final String PRO_PROFILE = "pro";

    @Autowired
    private ApplicationContext context;

    public String getActiveProfile() {
        return context.getEnvironment().getActiveProfiles()[0];
    }
}

點(diǎn)評:

ProfileConfig ,首先是作為一個(gè)相當(dāng)于Bean的形式存在著,此處的不在解釋@configuration和@component的區(qū)別;

注入ApplicationContext因?yàn)樵摻涌趀xtends于EnvironmentCapable,所以可以獲取到環(huán)境的一些信息;

以上就是SpringBoot中獲取profile的方法詳解的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot獲取profile的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java多線程系列之JDK并發(fā)包舉例詳解

    Java多線程系列之JDK并發(fā)包舉例詳解

    Java并發(fā)包提供了許多用于多線程編程的類和接口,這篇文章主要給大家介紹了關(guān)于Java多線程系列之JDK并發(fā)包的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-03-03
  • IntelliJ IDEA運(yùn)行bat腳本,自動(dòng)taskkill端口進(jìn)程

    IntelliJ IDEA運(yùn)行bat腳本,自動(dòng)taskkill端口進(jìn)程

    這篇文章主要介紹了IDEA里面無法運(yùn)行bat文件的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • SpringBoot訪問MongoDB數(shù)據(jù)庫的兩種方式

    SpringBoot訪問MongoDB數(shù)據(jù)庫的兩種方式

    MongoDB是一種非關(guān)系型數(shù)據(jù)庫,通過文檔存儲數(shù)據(jù),適用于大規(guī)模數(shù)據(jù)存儲和高并發(fā)訪問,這篇文章主要介紹了SpringBoot訪問MongoDB數(shù)據(jù)庫的兩種方式,感興趣想要詳細(xì)了解可以參考下文
    2023-05-05
  • springboot整合Atomikos的示例詳解

    springboot整合Atomikos的示例詳解

    這篇文章主要為大家詳細(xì)介紹了幾種分布式事務(wù)的解決方案的兩階段提交Atomikos,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下
    2024-11-11
  • Java過濾器@WebFilter用法詳解

    Java過濾器@WebFilter用法詳解

    @WebFilter用于將一個(gè)類聲明為過濾器,該注解將會(huì)在部署時(shí)被容器處理,容器將根據(jù)具體的屬性配置將相應(yīng)的類部署為過濾器,這篇文章主要給大家介紹了關(guān)于Java過濾器@WebFilter用法的相關(guān)資料,需要的朋友可以參考下
    2024-01-01
  • Windows環(huán)境IDEA下Ranger1.2.0源碼編譯詳細(xì)流程

    Windows環(huán)境IDEA下Ranger1.2.0源碼編譯詳細(xì)流程

    本文給大家講解Windows環(huán)境IDEA下Ranger1.2.0源碼編譯過程,通過配置Tomcat,發(fā)布?security-admin-web項(xiàng)目,編譯啟動(dòng)tomcat即可完成,需要的朋友參考下
    2021-06-06
  • ReentrantLock條件變量使多個(gè)線程順序執(zhí)行

    ReentrantLock條件變量使多個(gè)線程順序執(zhí)行

    這篇文章主要為大家介紹了ReentrantLock條件變量使多個(gè)線程順序執(zhí)行,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • SpringBoot打印POST請求原始入?yún)ody體方式

    SpringBoot打印POST請求原始入?yún)ody體方式

    這篇文章主要介紹了SpringBoot打印POST請求原始入?yún)ody體方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Springboot創(chuàng)建時(shí)常用的依賴詳解

    Springboot創(chuàng)建時(shí)常用的依賴詳解

    本文介紹了Spring?Boot項(xiàng)目中常用依賴的配置及作用,涵蓋了父依賴、Web應(yīng)用、測試、數(shù)據(jù)庫、MyBatis、連接池、JSON處理、Lombok、AOP、校驗(yàn)、監(jiān)控、工具包、打包配置、多配置文件以及熱部署等
    2025-03-03
  • 使用SpringBoot集成Thymeleaf和Flying?Saucer實(shí)現(xiàn)PDF導(dǎo)出

    使用SpringBoot集成Thymeleaf和Flying?Saucer實(shí)現(xiàn)PDF導(dǎo)出

    在?Spring?Boot?項(xiàng)目中,生成?PDF?報(bào)表或發(fā)票是常見需求,本文將介紹如何使用?Spring?Boot?集成?Thymeleaf?模板引擎和?Flying?Saucer?實(shí)現(xiàn)?PDF?導(dǎo)出,并提供詳細(xì)的代碼實(shí)現(xiàn)和常見問題解決方案,需要的朋友可以參考下
    2024-11-11

最新評論

会理县| 九江市| 乐至县| 嘉善县| 如东县| 濉溪县| 宕昌县| 雷山县| 潮安县| 平乐县| 布尔津县| 邹平县| 电白县| 澄江县| 民乐县| 社旗县| 油尖旺区| 廊坊市| 靖州| 泸西县| 柘荣县| 东源县| 鹤峰县| 永德县| 惠东县| 赫章县| 龙州县| 临高县| 巴彦淖尔市| 平山县| 北流市| 闽清县| 交城县| 和政县| 上犹县| 彰化市| 峨山| 通山县| 杭州市| 商城县| 海晏县|