• spring boot不同环境读取不同配置


    具体做法:

      • 不同环境的配置设置一个配置文件,例如:dev环境下的配置配置在application-dev.properties中;prod环境下的配置配置在application-prod.properties中。
      • 在application.properties中指定使用哪一个文件

        1、application-dev.properties(dev环境下的配置)
        [plain] view plain copy
         
        1. profile = dev_envrimont  
        2、application-prod.properties(prod环境下的配置)
        1 profile = prod_envrimont
        

        3、application.properties

        1 spring.data.mongodb.uri=mongodb://192.168.22.110:27017/myfirstMongodb
        2 
        3 #spring.profiles.active
        4 spring.profiles.active=dev

        4、Controller
        [java] view plain copy
         
        1.    @Autowired  
        2. 2     private Environment env;  
        3. 3           
        4. 4     @RequestMapping("/testProfile")  
        5. 5     public String testProfile(){  
        6. 6         return env.getProperty("profile");  
        7. 7     }  

        测试

        • 上述代码执行后的结果是:dev_envrimont和mongodb://192.168.22.110:27017/myfirstMongodb
        • 如果application.properties的配置改为:spring.profiles.active=prod,则结果是:prod_envrimont
        • 如果application.properties的配置改为:spring.profiles.active=prod,而application.properties中也配置了profile=xxx(不管该配置配置在spring.profiles.active=prod的上方还是下方),这个时候结果是:prod_envrimont
        • 如果application.properties的配置改为:spring.profiles.active=prod,而application.properties中也配置了profile=xxx(不管该配置配置在spring.profiles.active=prod的上方还是下方),但是application-prod.properties删掉了profile = prod_envrimont,这个时候结果是:xxx

        结论:

        • 各个环境公共的配置写在application.properties中
        • 各个模块独有的配置配置在自己的application-{xxx}.properties文件中
        • 程序读取的时候优先读取application.properties中选中的profile的配置,若读不到才会从application.properties去读
    • 本文转自:http://blog.csdn.net/lazycheerup/article/details/51915185

    spring boot中,可以通过在application.yml配置文件中,配置多个不同的profile,实现在不同的环境(比如开发、测试和生产环境)使用不同的配置变量。

    具体配置如下(application.yml中的内容):

    [plain] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. server:  
    2.   port: 8082  
    3.   
    4. # 默认的profile为dev,其他环境通过指定启动参数使用不同的profile,比如:  
    5. #   测试环境:java -jar my-spring-boot.jar --spring.profiles.active=test  
    6. #   生产环境:java -jar my-spring-boot.jar --spring.profiles.active=prod  
    7. spring:  
    8.   profiles:  
    9.     active: dev  
    10.   
    11. ---  
    12. # 开发环境配置  
    13. spring:  
    14.   profiles: dev  
    15. mysql:  
    16.   ipPort: localhost:3306  
    17.     
    18. ---  
    19. # 测试环境配置  
    20. spring:  
    21.   profiles: test  
    22. mysql:  
    23.   ipPort: 192.168.0.12:8066  
    24.     
    25. ---  
    26. # 生产环境配置  
    27. spring:  
    28.   profiles: prod  
    29. mysql:  
    30.   ipPort: 192.168.0.13:8066  


    使用方法:

    通过指定启动参数使用不同的profile,比如:
    #   测试环境:Java -jar my-spring-boot.jar --spring.profiles.active=test
    #   生产环境:java -jar my-spring-boot.jar --spring.profiles.active=prod

    源代码地址:https://github.com/xujijun/my-spring-boot

    本文转自:http://blog.csdn.net/clementad/article/details/51777621

  • 相关阅读:
    作为技术管理者,我如何保持技术判断力
    管理沟通
    管理规划
    nginx 在浏览器端保持cookie 一致
    openssl 升级操作 -2
    iptables 实际操作 之 规则查询 2
    iptables 概念 1
    openssl 升级 操作 -1
    使用秘钥对登录服务器
    SSH配置免秘钥登录
  • 原文地址:https://www.cnblogs.com/panxuejun/p/6743285.html
Copyright © 2020-2023  润新知