Spring Cloud 2.3.2 指定profiles配置(场景:不同环境激活不同配置)
1.开发步骤:
1. 修改test-profiles.yml
2. 修改启动类
指定配置文件路径:spring.config.location=classpath:/test-profiles.yml
@SpringBootApplication
public class TestProfiles {
public static void main(String[] args) {
ConfigurableApplicationContext context = new SpringApplicationBuilder(
TestProfiles.class)
.properties(
"spring.config.location=classpath:/test-profiles.yml")
.properties("spring.profiles.active=oracle").run(args);
// 输出变量
System.out.println(context.getEnvironment().getProperty("jdbc.driver"));
// 启动第二个Spring容器,指定端口为8081
ConfigurableApplicationContext context2 = new SpringApplicationBuilder(
TestProfiles.class)
.properties(
"spring.config.location=classpath:/test-profiles.yml")
.properties("spring.profiles.active=mysql").properties("server.port=8081").run(args);
// 输出变量
System.out.println(context2.getEnvironment().getProperty("jdbc.driver"));
}
}
3. 启动Spring boot 验证
第一个容器启动(默认端口8080 -- Tomcat initialized with port(s): 8080 (http))
第二个容器启动
2.源码:
项目源码路径:02/boot-config-file
Spring Cloud 全部源码 百度云:https://pan.baidu.com/s/1pTQC-MXDAoHqv0BU79uyaw

浙公网安备 33010602011771号