一、網上很多采用@Profile("dev")的方式獲取,但是這個是類級別的
二、開發中可能需要代碼級別
1、剛開始我想通過classpath下的文件讀取方式,麻煩死了,於是換了個思路。
2、SpringBoot啟動日志中有下面這句:
15:57:56.128 [restartedMain] INFO c.d.o.OptplatformApplication - The following profiles are active: test
(1)跟蹤代碼:SpringApplication.run方法
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
FailureAnalyzers analyzers = null;
configureHeadlessProperty();
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
Banner printedBanner = printBanner(environment);
context = createApplicationContext();
analyzers = new FailureAnalyzers(context);
prepareContext(context, environment, listeners, applicationArguments,
printedBanner); // 在這里打印了,跟蹤進去
refreshContext(context);
afterRefresh(context, applicationArguments);
listeners.finished(context, null);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
}
return context;
}
catch (Throwable ex) {
handleRunFailure(context, listeners, analyzers, ex);
throw new IllegalStateException(ex);
}
}
(2)跟蹤代碼:SpringApplication.prepareContext方法
private void prepareContext(ConfigurableApplicationContext context,
ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,
ApplicationArguments applicationArguments, Banner printedBanner) {
context.setEnvironment(environment);
postProcessApplicationContext(context);
applyInitializers(context);
listeners.contextPrepared(context);
if (this.logStartupInfo) {
logStartupInfo(context.getParent() == null);
logStartupProfileInfo(context); // 名稱很明顯,繼續跟蹤進去
}
......
}
(3)跟蹤代碼:SpringApplication.logStartupProfileInfo方法
protected void logStartupProfileInfo(ConfigurableApplicationContext context) {
Log log = getApplicationLog();
if (log.isInfoEnabled()) {
String[] activeProfiles = context.getEnvironment().getActiveProfiles();
if (ObjectUtils.isEmpty(activeProfiles)) {
String[] defaultProfiles = context.getEnvironment().getDefaultProfiles();
log.info("No active profile set, falling back to default profiles: "
+ StringUtils.arrayToCommaDelimitedString(defaultProfiles));
}
else {
log.info("The following profiles are active: "
+ StringUtils.arrayToCommaDelimitedString(activeProfiles)); //找到了,很明顯用了ApplicationContxt容器,接下來就是寫個工具類來獲取Application就行啦。
}
}
}
(4)編寫SpringContxtUtil工具類
/**
* 項目名稱:
* 類名: SpringContextUtil
* 描述: 獲取bean的工具類,可用於在線程里面獲取bean
* 創建人: awsm
* 創建時間: Dec 17, 2015 10:46:44 PM
* 修改人:little evil
* 修改時間:May 18, 2018 04:01:34 PM
* 修改備注:添加getActiveProfile方法,獲取當前環境
* 版本:1.1
*/
@Component
public class SpringContextUtil implements ApplicationContextAware {
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 {
this.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];
}
}
// 該工具類從網上抄來的,最后添加個獲取方法就完成了,這樣就能在代碼級別通過環境條件來控制方法行為了。
