spring 开发 测试 线上环境切换以及动态配置方法

配合maven  pom配置

<profiles>
   <profile>
      <id>local</id>
      <properties>
         <profile.active>local</profile.active>
      </properties>
      <activation>
         <activeByDefault>true</activeByDefault>
      </activation>
   </profile>
   <profile>
      <id>dev</id>
      <properties>
         <profile.active>dev</profile.active>
      </properties>
   </profile>
   <profile>
      <id>test</id>
      <properties>
         <profile.active>test</profile.active>
      </properties>
   </profile>
   <profile>
      <id>prod</id>
      <properties>
         <profile.active>prod</profile.active>
      </properties>
   </profile>
</profiles>

配置文件使用pom中的配置

spring 开发 测试 线上环境切换以及动态配置方法

spring:
  profiles:
    active: ${profile.active}

打包时指定使用环境参数以打test包环境为例:

mvn clean package -P test -DskipTests

 

Profile注解指定bean是否加载到spring容器中例如:@Profile({"dev","test","prod"}) 只在"dev","test","prod"上生效,其中的值与spring.profiles.active变量对比

spring 开发 测试 线上环境切换以及动态配置方法