spring 激活profile的几种方式

激活 profile

  spring 为我们提供了大量的激活 profile 的方法,可以通过代码来激活,也可以通过系统环境变量、JVM参数、servlet上下文参数来定义 spring.profiles.active 参数激活 profile,这里我们通过定义 JVM 参数实现。

1、ENV方式:

ConfigurableEnvironment.setActiveProfiles("test")

2、JVM参数方式:

  tomcat 中 catalina.bat(.sh中不用“set”) 添加JAVA_OPS。通过设置active选择不同配置文件

set JAVA_OPTS="-Dspring.profiles.active=test"

  eclipse 中启动tomcat。项目右键 run as –> run configuration–>Arguments–> VM arguments中添加。local配置文件不必上传git追踪管理

-Dspring.profiles.active="local"
  • 1

3、web.xml方式:

<init-param>
  <param-name>spring.profiles.active</param-name>
  <param-value>production</param-value>
</init-param>

4、标注方式(junit单元测试非常实用):
@ActiveProfiles({"unittest","productprofile"})

猜你喜欢

转载自jaesonchen.iteye.com/blog/2366018