zoukankan      html  css  js  c++  java
  • SpringBoot学习笔记(3)----SpringBoot的profile多环境配置

    在实际的应用开发中,可能会需要不同配置文件来支撑程序的运行,如开发,测试,预生产,生产环境等,程序在不同的环境下可能需要不同的配置,如不同数据源等,如果每次在不同的环境下都要去修改配置文件就会闲得不合理,而且很麻烦,此时就可以通过配置profiles,使程序在不同的环境中使用不同的配置文件。

    1. properties文件配置方式

      这种方式是将通用的配置到application.properties中,在application.properties中使用spring.profiles.active=xxx来指定某环境。每个不同环境的配置文件的命名方式为application-xxx.properties.其中spring.profiles.active=xxx的xxx与application-xxx.properties文件名的xxx必须一致。xxx就是表示是某个环境,程序会自动去读取application-xxx.properties中的配置文件信息。

      示例:

      application.properties

    spring.profiles.active=dev
    server.port=8080

      application-dev.properties

    server.port=8082

      启动项目查看日志:

    2018-10-11 15:54:59.672  INFO 7752 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8082 (http) with context path ''
    2018-10-11 15:54:59.684  INFO 7752 --- [  restartedMain] c.w.boot.SpringBootDemo02Application     : Started SpringBootDemo02Application in 2.504 seconds (JVM running for 4.081)

      可以看出项目的端口是8082,使用的是application-dev.properties配置的端口,而不是application.properties中的8080.

    2. yaml文件配置方式

      application.yaml

     
    server:
      port: 8080
    spring:
      profiles:
        active: dev
    ---
    spring:
      profiles: dev
    server:
      port: 8082
     

      yaml文件中,直接使用---符号,就可以进行多环境配置了,如上,使用了dev的环境,启动后端口仍然是8082。

    yaml配置文件的优势是,不用冗余多个文件,只需要一个文件就可以了,而且代码风格简洁,优雅,层次分明,但是不能使用@PropertySource()注解,所以如果需要使用@PropertySource()的话,需要使用application.properties.

    原文 SpringBoot学习笔记(3)----SpringBoot的profile多环境配置

  • 相关阅读:
    [HDU3247]Resource Archiver
    [POJ2243]考研路茫茫——单词情结
    [POJ2778]DNA Sequence
    [bzoj3670] [Noi2014]动物园
    [bzoj3786] 星系探索
    [bzoj1493] [NOI2007]项链工厂
    postgreSQL的设置自增主键初始值
    postgreSQL绝对值
    GitHub上新建或删除仓库Repository
    Intellij Idea上传本地项目到Git
  • 原文地址:https://www.cnblogs.com/xiaoshen666/p/10843921.html
Copyright © 2011-2022 走看看