企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
在开发中一般会配置不同的环境,这样方便切换不同的资源。如开发环境、测试环境、生产环境等。 [TOC] # 1. `.properties`多环境配置 `.properties`配置文件命名规范:`application-**.properties`。 <br/> **1. 不同环境的配置文件** (1)开发环境配置文件。 *`resources/application-dev.properties`* ```properties server.port=8080 ``` (2)生产环境配置文件。 *`resources/application-pro.properties`* ```properties server.port=9090 ``` (3)springboot默认配置文件,在该文件中决定使用哪个配置文件。 *`resources/application.properties`* ```properties #可以在默认的配置文件中配置共享的资源 server.servlet.context-path=/boot #使用生产环境的配置文件 spring.profiles.active=pro ``` **2. 测试** 启动项目,控制台会打印如下日志。 ``` : The following profiles are active: pro #当前使用的环境是pro : Tomcat initialized with port(s): 9090 (http) #当前环境的端口是9090 : Tomcat started on port(s): 9090 (http) with context path '/boot' #当前的项目根路径是/boot ``` **** 案例代码:https://gitee.com/flymini/codes01/tree/master/springboot_/com-learn-boot05 <br/> # 2.`.yml`多环境配置 **1. 多环境配置** 使用`.yml`进行多环境配置只需一个`resources/application.yml`文件即可。 ```yml spring: profiles: active: test # 使用测试环境的配置 #共享配置 server: servlet: context-path: /boot --- spring: profiles: dev #开发环境的配置 server: port: 8080 --- spring: profiles: test #测试环境的配置 server: port: 9090 ``` **2. 测试** 启动项目,控制台会打印如下日志。 ``` : The following profiles are active: test #当前使用的环境是test : Tomcat initialized with port(s): 9090 (http) #当前环境的端口是9090 : Tomcat started on port(s): 9090 (http) with context path '/boot' #当前的项目根路径是/boot ``` **** 案例代码:https://gitee.com/flymini/codes01/tree/master/springboot_/com-learn-boot06