SpringBoot配置切换

切换需求

有时候在本地测试是使用8080端口,可是上线使用的又是80端口。 此时就可以通过多配置文件实现多配置支持与灵活切换。

多配置文件

3个配置文件:
核心配置文件:application.properties
开发环境用的配置文件:application-dev.properties
生产环境用的配置文件:application-pro.properties
这样就可以通过application.properties里的spring.profiles.active 灵活地来切换使用哪个环境了

其他类同。

spring.mvc.view.prefix=/WEB-INF/jsp/

spring.mvc.view.suffix=.jsp

spring.profiles.active=pro

 

server.port=8080

server.context-path=/test

 

server.port=80

server.context-path=/

 

部署

不仅可以通过修改application.properties文件进行切换,还可以在部署环境下,指定不同的参数来确保生产环境总是使用的希望的那套配置。

cd C:\Users\X7TI\Downloads\springboot

mvn install

java -jar target/springboot-0.0.1-SNAPSHOT.jar --spring.profiles.active=pro

或者

java -jar target/springboot-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev

这样就可以保证在开发环境总是用的8080端口,而到了生产环境总是用的80端口,免去了每次上线还要修改端口号的麻烦。

posted @ 2019-07-16 22:57  Coding小马  阅读(321)  评论(0编辑  收藏  举报