1.开发步骤:
1. 修改启动类
指定配置文件路径: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"));
}
}
2. 启动Spring boot 验证
第一个容器启动
第二个容器启动
2.源码:
项目源码路径:02/boot-config-file
Spring Cloud 全部源码 百度云:https://pan.baidu.com/s/1pTQC-MXDAoHqv0BU79uyaw