1. 在Spring Boot中多环境配置文件名需要满足application-{profile}.properties
的格式,其中{profile}
对应你的环境标识,比如:
application-dev.properties:开发环境
application-test.properties:测试环境
application-prod.properties:生产环境
至于哪个具体的配置文件会被加载,需要在application.properties
文件中通过spring.profiles.active
属性来设置,其值对应{profile}
值。
如:spring.profiles.active=test
就会加载application-test.properties
配置文件内容
2. 可能遇到的问题:
如果自定义了日志文件, 如 logback-spring.xml , 并且在具体的 yml 或 properties 文件中 指定了该文件如:
logging:
config: classpath:logback-spring.xml
那么在 logback-spring.xml 文件中必须对 相应的Profile增加节点值才会在控制台有相应的输出.
<springProfile name="dev">
<root level="info">
<appender-ref ref="CONSOLE"/>
</root>
<logger name="com.example" level="debug"/>
</springProfile>
3. 其他
在pom.xml文件中也有一个 profile:
<profiles>
<profile>
<id>local</id>
<properties>
<profileActive>local</profileActive>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>onlinepre</id>
<properties>
<profileActive>onlinepre</profileActive>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<profileActive>prod</profileActive>
</properties>
</profile>
</profiles>
经过测试, 如果在 1. 中定义的profile如 application-dev.properties, 没有添加到pom.xml 的profile中, 项目实际运行时也不会出现问题. 不过很大概率是在我的项目中没有用到 pom.xml 相关的 profile ,比如 @Profiles 注解? 还需要进一步探究.