由于Springboot存在多个环境,就需要对生产、测试、开发等环境配置进行切换,Springboot两种配置文件类型:application.properties 和 application.yml,简单记录下用法,接着上一篇:https://blog.csdn.net/wx19900503/article/details/102959701
一、application.properties类型
新建三个环境的配置文件命令规则application-${env}.properties:定义不同的端口及user属性
代码结构如下:
application-dev.properties:
user.id=1
user.name=wuxi
user.description=学习springboot
server.port=8081
application-test.properties:
user.id=2
user.name=zj
user.description=学习mysql
server.port=8082
application-prod.properties:
user.id=3
user.name=cx
user.description=在出差
server.port=8083
application.properties配置如下:通过profile指定某个环境及配置文件生效
spring.profiles.active=dev
在Test中打印出user对象及查看springboot启动的端口号是否为开发环境配置的内容:
@SpringBootTest
class WxSpringbootPropertiesApplicationTests {
@Autowired
User user;
@Test
void contextLoads() {
System.out.println(user);
}
}
运行test查看输出内容:可以看到激活了dev配置
2019-11-06 16:19:08.092 INFO 1973 --- [ main] c.WxSpringbootPropertiesApplicationTests : Starting WxSpringbootPropertiesApplicationTests on MacBookPro with PID 1973 (started by wuxi in /Users/wuxi/Downloads/blog/wx-springboot/wx-springboot-properties)
2019-11-06 16:19:08.093 INFO 1973 --- [ main] c.WxSpringbootPropertiesApplicationTests : The following profiles are active: dev
2019-11-06 16:19:09.509 INFO 1973 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-11-06 16:19:09.851 INFO 1973 --- [ main] c.WxSpringbootPropertiesApplicationTests : Started WxSpringbootPropertiesApplicationTests in 2.274 seconds (JVM running for 3.577)
User{id=1, name='wuxi', description='学习springboot'}
2019-11-06 16:19:10.099 INFO 1973 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
Process finished with exit code 0
修改spring.profiles.active=prod测试:
2019-11-06 16:25:20.875 INFO 2047 --- [ main] c.WxSpringbootPropertiesApplicationTests : Starting WxSpringbootPropertiesApplicationTests on MacBookPro with PID 2047 (started by wuxi in /Users/wuxi/Downloads/blog/wx-springboot/wx-springboot-properties)
2019-11-06 16:25:20.876 INFO 2047 --- [ main] c.WxSpringbootPropertiesApplicationTests : The following profiles are active: prod
2019-11-06 16:25:22.229 INFO 2047 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-11-06 16:25:22.526 INFO 2047 --- [ main] c.WxSpringbootPropertiesApplicationTests : Started WxSpringbootPropertiesApplicationTests in 2.089 seconds (JVM running for 3.273)
User{id=2, name='wuxi', description='学习mysql'}
2019-11-06 16:25:22.748 INFO 2047 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
Process finished with exit code 0
二、application.yml文件进行配置:只要一个文件就可以,---分开不同环境配置,空格严格要求
# 指定激活环境
spring:
profiles:
active: prod
#开发环境
---
spring:
profiles: dev
user:
id: 1
name: wuxi
description: 学习springboot
server:
port: 8081
#生产环境
---
spring:
profiles: prod
user:
id: 2
name: zj
description: 学习mysql
server:
port: 8082
#测试环境
---
spring:
profiles: test
user:
id: 3
name: cx
description: 在出差
server:
port: 8083
将application.properties config文件夹重命名,运行测试类:两者配置会优先properties
测试通过,检查端口为8082是prod配置
如果ymal 未指定激活的配置,则运行jar包时可以指定:
java -jar xx --spring.profiles.active=xx
写完文章突然发现一个问题
就是我不管切哪个环境,我的name都=wuxi,然后各种检查代码,发现都没问题,主要我dev配的wuxi造成了误导
最后找到问题所在,这个user.name是我电脑用户名所以不管怎么修改 都是wuxi
获取当前pid,jinfo pid:user.name=wuxi,JVM运行参数恒等于wuxi
验证问题,将配置文件user改成User试试