第九课 SpringBoot2基础-多环境profile和原理解析
tags:
- Spring Boot
- 2021尚硅谷
- 雷丰阳
文章目录
第一节 Profile功能
1.1 application-profile功能
- 为了方便多环境适配,springboot简化了profile功能。
- 默认配置文件 application.yaml;任何时候都会加载,通过配置
spring.profiles.active=prod。激活指定环境 - 指定环境配置文件 application-{env}.yaml
- 激活指定环境
- 配置文件激活。
- 命令行激活:
java -jar xxx.jar --spring.profiles.active=prod --person.name=haha
- 修改配置文件的任意值,命令行优先
- 默认配置与环境配置同时生效
- 同名配置项,profile配置优先
1.2 @Profile条件装配功能
- 请求person接口, 不同环境装配不同的类。
package com.atguigu.boot.controller;
import com.atguigu.boot.bean.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private Person person;
@GetMapping("/person")
public String person(){
return person.getClass().toString();
}
}
- 生产环境下绑定。
package com.atguigu.boot.bean;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
@Profile("prod")
@Component
@ConfigurationProperties("person")
@Data
public class Boss implements Person{
private String name;
private Integer age;
}
- 测试环境下绑定。
package com.atguigu.boot.bean;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
@Profile("test")
@Component
@ConfigurationProperties("person")
@Data
public class Worker implements Person {
private String name;
private Integer age;
}
- 接口
package com.atguigu.boot.bean;
public interface Person {
String getName();
Integer getAge();
}
@Profile("test")不仅仅可以夹在类上,也可以加在配置方法上。
package com.atguigu.boot.config;
import com.atguigu.boot.bean.Color;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
//@Configuration
public class MyConfig {
@Profile("prod")
@Bean
public Color red(){
return new Color();
}
@Profile("test")
@Bean
public Color green(){
return new Color();
}
}
1.3 profile分组
- 指定激活myprod组,myprod包括ppd和prod俩个配置文件同时加载。
server.port=8080
spring.profiles.active=myprod
spring.profiles.group.myprod[0]=ppd
spring.profiles.group.myprod[1]=prod
spring.profiles.group.mytest[0]=test
第二节 外部化配置
2.1 外部化配置源
- https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config
- 外部配置源常用:Java属性文件、YAML文件、环境变量、命令行参数;
- 可以通过@value直接获取环境变量中的值和系统的属性。
@RestController
public class HelloController {
@Value("${WORKON_HOME}")
private String msg;
@Value("${sun.desktop}")
private String osName;
@GetMapping("/msg")
public String getMsg(){
return msg;
}
}
- 可以在启动类上看到系统的环境变量和系统的属性。
package com.atguigu.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import java.util.Map;
@SpringBootApplication
public class Boot09FeaturesProfileApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(Boot09FeaturesProfileApplication.class, args);
ConfigurableEnvironment environment = run.getEnvironment();
Map<String, Object> systemEnvironment = environment.getSystemEnvironment();
Map<String, Object> systemProperties = environment.getSystemProperties();
System.out.println(systemEnvironment);
System.out.println(systemProperties);
}
}
2.2 配置文件查找位置
- classpath 根路径
- classpath 根路径下config目录
- jar包当前目录
- jar包当前目录的config目录
- /config子目录的直接子目录
- 后面的会覆盖前面的优先级。
2.3 配置文件加载顺序
- 当前jar包内部的application.properties和application.yml
- 当前jar包内部的application-{profile}.properties 和 application-{profile}.yml
- 引用的外部jar包的application.properties和application.yml
- 引用的外部jar包的application-{profile}.properties 和 application-{profile}.yml
- 指定环境优先,外部优先,后面的可以覆盖前面的同名配置项
第三节 自定义starter
3.1 自定义starter
- starter原理:
- starter-pom引入 autoconfigurer 包。
- autoconfigure包中配置使用 META-INF/spring.factories 中 EnableAutoConfiguration 的值,使得项目启动加载指定的自动配置类
- 编写自动配置类 xxxAutoConfiguration -> xxxxProperties
- @Configuration
- @Conditional
- @EnableConfigurationProperties
- @Bean

- 需要创建下面两个。直接看代码。
- atguigu-hello-spring-boot-starter(启动器)引入下面自动配置包。
- atguigu-hello-spring-boot-starter-autoconfigure(自动配置包)
- 这里要写
META-INF\spring.factories让他自动加载。
- 这里要写
3.2 SpringBoot启动过程
-
创建 SpringApplication,主要把一些关键的组件提前读取过来。
- 保存一些信息。
- 判定当前应用的类型。ClassUtils。Servlet
- bootstrappers:初始启动引导器
(List<Bootstrapper>):去spring.factories文件中找 org.springframework.boot.Bootstrapper - 找 ApplicationContextInitializer;去spring.factories找
ApplicationContextInitializer List<ApplicationContextInitializer<?>> initializers - 找 ApplicationListener ;应用监听器。去spring.factories找
ApplicationListener List<ApplicationListener<?>> listeners
-
运行 SpringApplication
- StopWatch
- 记录应用的启动时间
- 创建引导上下文(Context环境)createBootstrapContext()
- 获取到所有之前的** bootstrappers** 挨个执行 intitialize() 来完成对引导启动器上下文环境设置
- 让当前应用进入headless模式。java.awt.headless
- 获取所有 RunListener(运行监听器)【为了方便所有Listener进行事件感知】
getSpringFactoriesInstances(只要看到这个方法就是) 去spring.factories找 SpringApplicationRunListener.- 遍历
SpringApplicationRunListener调用 starting 方法;(运行时监听器) - 相当于通知所有感兴趣系统正在启动过程的人,项目正在 starting。
- 保存命令行参数;ApplicationArguments
- 准备环境 prepareEnvironment();
- 返回或者创建基础环境信息对象。StandardServletEnvironment
- 配置环境信息对象。
- 读取所有的配置源的配置属性值。
- 绑定环境信息
- 监听器调用 listener.environmentPrepared();通知所有的监听器当前环境准备完成
- 创建IOC容器(createApplicationContext()) 非常重要。
- 根据项目类型(Servlet)创建容器,
- 当前会创建 AnnotationConfigServletWebServerApplicationContext
- 准备ApplicationContext IOC容器的基本信息 prepareContext()
- 保存环境信息
- IOC容器的后置处理流程。
- 应用初始化器;applyInitializers;
- 遍历所有的
ApplicationContextInitializer。调用 initialize.。来对ioc容器进行初始化扩展功能 - 遍历所有的 listener 调用 contextPrepared。
EventPublishRunListenr;通知所有的监听器contextPrepared
- 遍历所有的
- 所有的监听器 调用 contextLoaded。通知所有的监听器 contextLoaded;
- 刷新IOC容器。refreshContext
- 创建容器中的所有组件(Spring注解)
- 容器刷新完成后工作?afterRefresh
- 所有监听器 调用
listeners.started(context); 通知所有的监听器 started - 调用所有runners;callRunners()
- 获取容器中的 ApplicationRunner
- 获取容器中的 CommandLineRunner
- 合并所有runner并且按照@Order进行排序
- 遍历所有的runner。调用 run 方法
- 如果以上有异常,
- 调用Listener 的 failed
- 调用所有监听器的 running 方法
listeners.running(context);通知所有的监听器 running - running如果有问题。继续通知 failed 。调用所有 Listener 的 failed;通知所有的监听器 failed
3.3 Application Events and Listeners
- https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-application-events-and-listeners
ApplicationContextInitializer
ApplicationListener
SpringApplicationRunListener
349

被折叠的 条评论
为什么被折叠?



