一.问题说明
1.springboot多环境可以由spring.profiles.active来控制测试、生产等环境
2.通过pom.xml中的maven配置来控制springboot的多环境
3.如果报下面异常,需要在idea的maven窗口reload
org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token found character '@' that cannot start any token. (Do not use @ for indentation)
4.如果idea启动没有见到控制台环境变化,需要在maven窗口中点击compile重试,是idea的bug,高版本idea在maven窗口中会有Profiles选项框,无此情况,
二.代码示例
1.创建application.yaml
spring:
profiles:
active: @profile.active@
2.创建application-test.yaml
server:
port: 8081
3.创建application-prod.yaml
server:
port: 8082
4.pom.xml中添加配置
<!--设置多环境-->
<profiles>
<profile>
<id>env_test</id>
<properties>
<profile.active>test</profile.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>env_prod</id>
<properties>
<profile.active>prod</profile.active>
</properties>
</profile>
</profiles>
5.切换pom.xml的环境
1.切换pom.xml中的配置后,打包后检查application.yaml中实际的值
2.idea也可以运行检查控制台启动的端口号
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/92350.html