SpringBoot中獲取profile的方法詳解
阿新 • • 發佈:2022-05-19
這篇文章主要介紹了springboot獲取profile的操作,文中的示例程式碼講解詳細,具有很好的參考價值,希望對大家有所幫助
spring boot與profile
spring boot 的專案中不再使用xml的方式進行配置,並且,它還遵循著約定大於配置。
靜態獲取方式
靜態工具類獲取當前專案的profile環境。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
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 ([email protected])
* @date 2018/10/27
* 專案名稱:
* 類名: 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());
}
// 獲取當前環境
public static String getActiveProfile() {
return context.getEnvironment().getActiveProfiles()[ 0 ];
}
}
|
點評:
這種方式在使用起來很方便也是現在各個部落格文章所撰寫的方式,在很多Service的業務程式碼中使用起來很方便,畢竟是靜態的方式嘛!
spring中的這個介面是在所有的Bean注入完畢,才會執行setApplicationContext方法,那麼問題來了,往往在專案中我們可能會對一些Bean進行一些config操作,例如:@Bean注入,而有時候我們會根據不同的profile進行不同的定製化config。這個時候恰恰我們的工具類SpringContextUtil還沒有執行setApplicationContext此時工具類中的context物件還是null。就會出現異常的情況。下面的方式可以彌補這個缺陷。
autowire ProfileConfig
使用這種方式首先宣告一下,其實就相當於一個特殊的configBean一樣,因為只有這樣,這個類才不會在所有bean全部載入完畢後才能獲取到context。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
/**
* @author wangjiuzhou ([email protected])
* @date 2018/11/07
*
* 獲取當前專案環境: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 ];
}
}
|
點評:
ProfileConfig ,首先是作為一個相當於Bean的形式存在著,此處的不在解釋@configuration和@component的區別;
注入ApplicationContext因為該介面extends於EnvironmentCapable,所以可以獲取到環境的一些資訊;
以上就是SpringBoot中獲取profile的方法詳解的詳細內容,更多關於SpringBoot獲取profile的資料請關注指令碼之家其它相關文章!
轉自:https://www.jb51.net/article/243690.htm