一、网上很多采用@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];
}
}
// 该工具类从网上抄来的,最后添加个获取方法就完成了,这样就能在代码级别通过环境条件来控制方法行为了。

  

SpringBoot中获取spring.profiles.active的值的更多相关文章

  1. SpringBoot利用spring.profiles.active=@spring.active@不同环境下灵活切换配置文件

    一.创建配置文件 配置文件结构:这里建三个配置文件,application.yml作为主配置文件配置所有共同的配置:-dev和-local分别配置两种环境下的不同配置内容,如数据库地址等. appli ...

  2. spring程序打包war,直接通过-jar启动,并指定spring.profiles.active参数控制多环境配置

    备注:spring boot有内嵌tomcat,jar项目可以用java -jar命令启动,war包也可以,且可以直接指定spring.profiles.active参数控制多环境配置 直接指定传参, ...

  3. spring boot 入门 使用spring.profiles.active来分区配置

    很多时候,我们项目在开发环境和生成环境的环境配置是不一样的,例如,数据库配置,在开发的时候,我们一般用测试数据库,而在生产环境的时候,我们是用正式的数据,这时候,我们可以利用profile在不同的环境 ...

  4. spring boot 入门 使用spring.profiles.active来分区配置(转)

    很多时候,我们项目在开发环境和生成环境的环境配置是不一样的,例如,数据库配置,在开发的时候,我们一般用测试数据库,而在生产环境的时候,我们是用正式的数据,这时候,我们可以利用profile在不同的环境 ...

  5. 2505-springboot使用spring.profiles.active来分区配置

    参考文献: spring boot 入门 使用spring.profiles.active来分区配置 http://www.leftso.com/blog/111.html 很多时候,我们项目在开发环 ...

  6. springboot中spring.profiles.active来引入多个properties文件 & Springboot获取容器中对象

    1.    引入多个properties文件 很多时候,我们项目在开发环境和生成环境的环境配置是不一样的,例如,数据库配置,在开发的时候,我们一般用测试数据库,而在生产环境的时候,我们是用正式的数据, ...

  7. spring-boot:run启动时,指定spring.profiles.active

    Maven启动指定Profile通过-P,如mvn spring-boot:run -Ptest,但这是Maven的Profile. 如果要指定spring-boot的spring.profiles. ...

  8. SpringBoot application.yml logback.xml,多环境配置,支持 java -jar --spring.profiles.active

    趁今天有时间整理了一下 启动命令为 //开发环境 java -jar app.jar --spring.profiles.active=dev--server.port=8060 //测试环境 jav ...

  9. SpringBoot application.yml logback.xml,多环境配置,支持 java -jar --spring.profiles.active(转)

    趁今天有时间整理了一下 启动命令为 //开发环境 java -jar app.jar --spring.profiles.active=dev--server.port=8060 //测试环境 jav ...

随机推荐

  1. Spring入门IOC和AOP学习笔记

    Spring入门IOC和AOP学习笔记 概述 Spring框架的核心有两个: Spring容器作为超级大工厂,负责管理.创建所有的Java对象,这些Java对象被称为Bean. Spring容器管理容 ...

  2. YouTube 网站的架构演进——阅读心得

    基础平台 Apache Python Linux(SuSe) MySQL psyco,一个动态的Python到C的编译器 lighttpd代替Apache做视频播放 状态 支持每天超过5亿的视频点击量 ...

  3. kotlin中对象表达式

    在kotlin中,也有类似的功能,但不是匿名类,而是对象,需要使用object关键字,对象要继承的列需要与object之间的冒号(:)分隔. fun main(arg: Array<String ...

  4. Python - social-auth-app-django 模块 - 商城项目 第三方方式登录 - 微博

    开发准备 开通微博开发者权限 点击这里 进入 微博开放平台 开通后权限后创建应用 创建网页应用, 此处不需要进行审核即可使用测试环境 开发环境信息 此处一些信息是很重要的东西, 比如 App_key ...

  5. VBA添加下拉菜单

    Sub createMenus() Dim cmdBar As CommandBar Dim cmdMenu As CommandBarPopup Dim cmdBtn As CommandBarBu ...

  6. 阶段5 3.微服务项目【学成在线】_day02 CMS前端开发_05-vuejs研究-vuejs基础-v-text指令

    把js移到body 的下面 网速改慢一点 通过模拟网速慢的情况.刷新页面的时候会有闪烁的效果 速度快的情况下也会闪烁 ,只不过是不明显. 2.解决插值表达式闪烁问题,使用v-text v-text可以 ...

  7. org/springframework/cache/jcache/config/AbstractJCacheConfiguration.class

    在使用Spring-MVC环境时  报错: Failed to parse configuration class [org.springframework.cache.aspectj.AspectJ ...

  8. 【c++基础】C++编写Config类读取配置文件

    前言 系统程序一般需要读取参数文件,看到一个很好的Config类记录在此. 头文件Config.h //Config.h //re: https://blog.csdn.net/David_xtd/a ...

  9. Vue 组件基础完整示例2

    简介此页面可以直接复制运行,包含以下应用: Vue slot插槽使用Vue v-model使用Vue props使用父子组件数据传递element-ui使用HTML方式注册子组件,可以将子组件数据写在 ...

  10. 一次Python操作数据库和excel过程

    师从‘百测’besttest 最近牛大湿教了操作数据库和操作excel,写了一个小小的脚本,传入一个表名后,将表中所有数据写入excel中. 使用了pymysql,xlwt,需要自行安装. impor ...