springboot通过profile设置配置文件

写在前面

本文直接在已存在springboot项目基础上测试,关于如何创建springboot项目,点击这里。本文源码点击这里

1:格式说明

application-{profile}.yml,或者是application-{profile}.properties也可以。

2:通过修改配置文件

2.1:准备配置文件

  • application.properties
  • application-dev.properties
  • application-test.properties
    其中application-test.properties内容如下:
server.port=9876

即启用9876端口。
application-dev.properties内容如下:

server.port=9875

2.2:启用test

  • 修改application.properties:
    spring.profiles.active=test
  • 启动测试
The following profiles are active: test

2.3:启用dev

  • 修改application.properties
    spring.profiles.active=dev
  • 启动测试
The following profiles are active: dev

3:jar方式

3.1:打包

通过package打包,如下图:
在这里插入图片描述

3.2:指定test

xbdeMacBook-Air:target xb$ java -jar springboot-helloworld-0.0.1-SNAPSHOT.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v2.2.10.RELEASE)

...
2020-10-21 14:50:09.282  INFO 41047 --- [           main] d.d.s.SpringbootHelloworldApplication    : The following profiles are active: dev

3.3:指定dev

xbdeMacBook-Air:target xb$ java -jar springboot-helloworld-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v2.2.10.RELEASE)

...
2020-10-21 14:57:32.115  INFO 41390 --- [           main] d.d.s.SpringbootHelloworldApplication    : The following profiles are active: dev

最后:都让开,我要喝瑞幸

在这里插入图片描述


版权声明:本文为wang0907原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。