一、网上很多采用@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. 怎样在VMware虚拟机中使用安装并设置Ubuntu系统

    1 2 3 4 5 6 7 分步阅读 Ubuntu 系统是一款优秀的.基于GNU/Linux 的平台的桌面系统. 当然,目前为止很多应用程序还完全不能允许运行在 Ubuntu 系统上,而且 Ubunt ...

  2. linux Host key verification failed.错误

    Host key verification failed. 1. ssh-keygen -R 你要访问的IP地址 2. ssh-keygen -R 108.61.163.242

  3. Facebook币Libra学习-5.Move组织目录

    Move是一种新的编程语言,旨在为Libra Blockchain提供安全可编程的基础. 组织 Move语言目录由五部分组成: 的虚拟机(VM),其中包含的字节码格式,字节码解释器,和基础设施执行事务 ...

  4. Oracle 的 oracle 数据库分类

    一.数据库分类 1.小型数据库:access.foxbase 2.中型数据库:informix.sql server.mysql 3.大型数据库:sybase.db2.oracle 二.项目中如何合理 ...

  5. [Java读书笔记] Effective Java(Third Edition) 第 6 章 枚举和注解

    Java支持两种引用类型的特殊用途的系列:一种称为枚举类型(enum type)的类和一种称为注解类型(annotation type)的接口. 第34条:用enum代替int常量 枚举是其合法值由一 ...

  6. OOM异常的发生原因

    一,jvm内存区域 1,程序计数器 一块很小的内存空间,作用是当前线程所执行的字节码的行号指示器. 2,java栈 与程序计数器一样,java栈(虚拟机栈)也是线程私有的,其生命周期与线程相同.通常存 ...

  7. vector swap

    #include <iostream>#include <vector>#include <list>#include <deque> using na ...

  8. labelme

    项目:https://github.com/wkentaro/labelme?tdsourcetag=s_pcqq_aiomsg 说明:https://www.bilibili.com/video/a ...

  9. JAVA 基础编程练习题29 【程序 29 求矩阵对角线之和】

    29 [程序 29 求矩阵对角线之和] 题目:求一个 3*3 矩阵对角线元素之和 程序分析:利用双重 for 循环控制输入二维数组,再将 a[i][i]累加后输出. package cskaoyan; ...

  10. Swift加载Xib创建的Controller

    Xib显示如下: <注意箭头处即可> 按住Control键,点击Files'owner拖动到View即可. 加载该控制器如下: func registerClick() { let reg ...