SpringBoot系列教材 (九)- springboot 如何进行多配置切换

SpringBoot系列教材 (九)- 基础 - springboot 如何进行多配置切换


步骤1:切换需求
步骤2:多配置文件
步骤3:部署

步骤 1 : 切换需求

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

步骤 2 : 多配置文件

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=/

步骤 3 : 部署

不仅可以通过修改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端口,免去了每次上线还要修改端口号的麻烦。
参考: 创建可运行Springboot jar包教程

部署


更多内容,点击了解: http://how2j.cn/k/springboot/springboot-switch-config/1644.html

猜你喜欢

转载自blog.csdn.net/wanghuiwei888/article/details/88080767