Spring boot 激活 profile的几种方式

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

Spring boot 激活 profile的几种方式

  1. 在配置文件中直接指定

     spring.profiles.active=test
    

    这种方式非常不灵活,在实际开发部不太会使用到

  2. 使用占位符,在打包时替换,以mavne为例:

    首先在配置文件中增加:

     [email protected]@
    

    在pom.xml中增加不同环境打包的配置:

       
         
           dev
           
             dev
           
           
             true
           
           
             
               org.springframework.boot
               spring-boot-devtools
               true
             
             
               io.springfox
               springfox-swagger-ui
               2.6.1
             
           
         
         
           prod
           
             prod
           
         
         
           test
           
             test
           
           
             
               io.springfox
               springfox-swagger-ui
               2.6.1
             
           
         
       
       
         
           
             org.springframework.boot
             spring-boot-maven-plugin
           
         
         
           
             src/main/resources
             true
           
         
       
    

    执行打包命令:

     mvn package -Ptest  
    

    缺点:每次打包都要指定profile

  3. JVM参数方式:

    java命令行:

     java -jar app.jar --spring.profiles.active=dev
    

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

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

    eclipse 中启动tomcat。项目右键 run as –> run configuration–>Arguments–> VM arguments中添加。

     -Dspring.profiles.active="dev"
    
  4. web.xml方式

     
       spring.profiles.active
       production
     
    
  5. 标注方式(junit单元测试非常实用)

     @ActiveProfiles({"unittest","productprofile"})
    
  6. ENV方式(建议使用此方式)

    系统环境变量SPRING_PROFILES_ACTIVE(注意:是大写)

    优点:

    1. 应用不需要关注环境,环境应该有主机或者容器来定义;
    2. 一台主机部署多个应用不需要在每个Tomcat的setenv.sh里面增加配置,或者在每个启动脚本里去增加:-Dspring.profiles.active=prod。本地Windows调试的时候,也不想在eclipse里去设置环境变量,随便右键debug即可;
    3. 只需要打一个包就可以在所有环境运行,可以避免因打错包或部署错包造成的一系列问题。

转载于:https://my.oschina.net/u/1469495/blog/1522784

你可能感兴趣的:(Spring boot 激活 profile的几种方式)