欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot中獲取profile的方法詳解

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

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)評(píng):

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

但是有一種缺陷,因?yàn)閷?shí)現(xiàn)ApplicationContextAware接口,而spring中的這個(gè)接口是在所有的Bean注入完畢,才會(huì)執(zhí)行setApplicationContext方法,那么問題來了,往往在項(xiàng)目中我們可能會(huì)對(duì)一些Bean進(jìn)行一些config操作,例如:@Bean注入,而有時(shí)候我們會(huì)根據(jù)不同的profile進(jìn)行不同的定制化config。這個(gè)時(shí)候恰恰我們的工具類SpringContextUtil還沒有執(zhí)行setApplicationContext此時(shí)工具類中的context對(duì)象還是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)評(píng):

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

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

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

相關(guān)文章

最新評(píng)論