目录
第十二章 原理解析
12.1 Profile功能
12.1.1 application-profile功能
12.1.2 @Profile条件装配功能
12.1.3 profile分组
12.2 外部化配置
12.2.1 外部配置源
12.2.2 配置文件查找位置
12.2.3 配置文件加载顺序
12.2.4 关键
12.3 自定义starter
12.3.1 starter启动原理
12.3.2 自定义starter
12.4 SpringBoot原理
12.4.1 SpringBoot启动过程
12.4.2 Application Events and Listeners
12.4.3 ApplicationRunner 与 CommandLineRunner
为了方便多环境适配,springboot简化了profile功能,简化了环境的切换导致需要修改配置文件
server.port=8080 #同名配置 #在配置文件中指定激活的环境,默认配置文件和指定环境的配置文件都会生效,但是同名属性会覆盖 spring.profiles.active=prod
当加上了@Profile注解,可以使某个环境的条件下这个组件才生效:
public interface Person {
String getName();
Integer getAge();
}
@Profile(value = {"prod","default"}) //prod、default环境下生效
@Component
@ConfigurationProperties("person")//读取配置文件中person开头的值
@Data
public class Boss implements Person {
private String name;
private Integer age;
}
@Profile("test")
@Component
@ConfigurationProperties("person")
@Data
public class Worker implements Person {
private String name;
private Integer age;
}
除了标在类上,也可以标在方法上:
@Configuration
public class MyConfig {
@Profile("prod")
@Bean
public Color red(){
return new Color();
}
@Profile("test")
@Bean
public Color green(){
return new Color();
}
}
spring.profiles.active=myprod #生产环境组 spring.profiles.group.myprod[0]=ppd spring.profiles.group.myprod[1]=prod #测试环境组 spring.profiles.group.mytest[0]=test
Core Features
比如把数据库的登录信息等不是固定写死在配置文件中,而实抽取出现写在一个外部配置文件中
常用:Java属性文件、YAML文件、环境变量、命令行参数
比如获取环境变量的值:
@RestController
public class HelloController {
@Value("${MAVEN_HOME}")
private String msg;
@GetMapping("/msg")
public String getMsg(){
return msg+"==>"+osName;
}
}
获取系统的环境变量和属性:
@SpringBootApplication
public class Boot09FeaturesProfileApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(Boot09FeaturesProfileApplication.class, args);
ConfigurableEnvironment environment = run.getEnvironment();
Map systemEnvironment = environment.getSystemEnvironment();
Map systemProperties = environment.getSystemProperties();
System.out.println(systemEnvironment);
System.out.println(systemProperties);
}
}
application.properties和application.yaml文件读取位置:
classpath 根路径
classpath 根路径下config目录
jar包当前目录
jar包当前目录的config目录
/config子目录的直接子目录
当前jar包内部的application.properties和application.yml
当前jar包内部的application-{profile}.properties和application-{profile}.yml
引用的外部jar包的application.properties和application.yml
引用的外部jar包的application-{profile}.properties和application-{profile}.yml
指定环境优先,外部优先,后面的可以覆盖前面的的同名配置项
atguigu-hello-spring-boot-starter(启动器)
atguigu-hello-spring-boot-starter-autoconfigure(自动配置包)
pom.xml文件:
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.4.0
com.atguigu
atguigu-hello-spring-boot-starter-autoconfigure
0.0.1-SNAPSHOT
atguigu-hello-spring-boot-starter-autoconfigure
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter
在配置文件中自动注入需要的类:
/**
* 默认不要放在容器中
*/
public class HelloService {
@Autowired
HelloProperties helloProperties;
public String sayHello(String userName){
return helloProperties.getPrefix() + ":"+userName+"》"+helloProperties.getSuffix();
}
}
@ConfigurationProperties("atguigu.hello")
public class HelloProperties {
private String prefix;
private String suffix;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
@Configuration
@EnableConfigurationProperties(HelloProperties.class) //默认HelloProperties放在容器中
public class HelloServiceAutoConfiguration{
@ConditionalOnMissingBean(HelloService.class)
@Bean
public HelloService helloService(){
HelloService helloService = new HelloService();
return helloService;
}
}
在类路径下创建一个META-INF文件夹,这个文件夹下有一个文件spring.factories:
# Auto Configure # 指定项目一启动要加载哪个包哪个自动配置类 org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.atguigu.hello.auto.HelloServiceAutoConfiguration
此时可以在其他项目中引入这个自定义的starter场景:
com.atguigu atguigu-hello-spring-boot-starter 1.0-SNAPSHOT
#在配置文件中定义属性atguigu.hello开头 atguigu.hello.prefix=ATGUIGU atguigu.hello.suffix=88888
SpringBoot原理【Spring注解】、SpringMVC原理、自动配置原理、SpringBoot原理
bootstrappers初始启动引导器(List
准备环境prepareEnvironment()
配置环境信息对象
读取所有的配置源的配置属性值
根据项目类型创建容器,一般为Servlet
IOC容器的后置处理流程
应用初始化器applyInitializers
容器刷新完成后工作,afterRefresh
调用所有runners,callRunners()
如果以上有异常
Core Features
ApplicationContextInitializer
ApplicationListener
SpringApplicationRunListener
可以自动义组件:在启动过程中某些时机增添自己需要的功能
public class MyApplicationContextInitializer implements ApplicationContextInitializer {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
System.out.println("MyApplicationContextInitializer ....initialize.... ");
}
}
public class MyApplicationListener implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent event) {
System.out.println("MyApplicationListener.....onApplicationEvent...");
}
}
public class MySpringApplicationRunListener implements SpringApplicationRunListener {
private SpringApplication application;
public MySpringApplicationRunListener(SpringApplication application, String[] args){
this.application = application;
}
@Override
public void starting(ConfigurableBootstrapContext bootstrapContext) {
System.out.println("MySpringApplicationRunListener....starting....");
}
@Override
public void environmentPrepared(ConfigurableBootstrapContext bootstrapContext, ConfigurableEnvironment environment) {
System.out.println("MySpringApplicationRunListener....environmentPrepared....");
}
@Override
public void contextPrepared(ConfigurableApplicationContext context) {
System.out.println("MySpringApplicationRunListener....contextPrepared....");
}
@Override
public void contextLoaded(ConfigurableApplicationContext context) {
System.out.println("MySpringApplicationRunListener....contextLoaded....");
}
@Override
public void started(ConfigurableApplicationContext context) {
System.out.println("MySpringApplicationRunListener....started....");
}
@Override
public void running(ConfigurableApplicationContext context) {
System.out.println("MySpringApplicationRunListener....running....");
}
@Override
public void failed(ConfigurableApplicationContext context, Throwable exception) {
System.out.println("MySpringApplicationRunListener....failed....");
}
}
runner接口:
@FunctionalInterface
public interface ApplicationRunner {
/**
* Callback used to run the bean.
* @param args incoming application arguments
* @throws Exception on error
*/
void run(ApplicationArguments args) throws Exception;
}
@FunctionalInterface
public interface CommandLineRunner {
/**
* Callback used to run the bean.
* @param args incoming main method arguments
* @throws Exception on error
*/
void run(String... args) throws Exception;
}
可以自定义组件:
@Order(1)
@Component
public class MyApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("MyApplicationRunner...run...");
}
}
/**
* 应用启动做一个一次性事情
*/
@Order(2)
@Component
public class MyCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("MyCommandLineRunner....run....");
}
}
PS:根据尚硅谷视频整理,如有侵权,联系删除