SpringBoot2(八):原理解析

1.Profile功能:为了方便多环境适配,springboot简化了profile功能

        1.application-profile功能

                1.默认配置文件 application.yaml;任何时候都会加载

                2.指定环境配置文件 application-{env}.yaml

                3.激活指定环境

                        1.配置文件激活

                        2.命令行激活:java -jar xxx.jar --spring.profiles.active=prod --person.name=haha

                                1.修改配置文件的任意值,命令行优先

                4.默认配置与环境配置同时生效

                5.同名配置项,profile配置优先

        2.@Profile条件装配功能

@Configuration(proxyBeanMethods = false)
@Profile("production")
public class ProductionConfiguration {

    // ...

}

        3.profile分组

spring.profiles.group.production[0]=proddb
spring.profiles.group.production[1]=prodmq

使用:--spring.profiles.active=production  激活

2.外部化配置:Core Features

        1.配置

                1.Default properties (specified by setting SpringApplication.setDefaultProperties).

                2.@PropertySource annotations on your @Configuration classes. Please note that such property sources are not added to the Environment until the application context is being refreshed. This is too late to configure certain properties such as logging.* and spring.main.* which are read before refresh begins.

                3.Config data (such as application.properties files)

                4.A RandomValuePropertySource that has properties only in random.*.

                5.OS environment variables.

                6.Java System properties (System.getProperties()).

                7.JNDI attributes from java:comp/env.

                8.ServletContext init parameters.

                9.ServletConfig init parameters.

                10.Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).

                11.Command line arguments.

                12.properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application.

                13.@TestPropertySource annotations on your tests.

                14.Devtools global settings properties in the $HOME/.config/spring-boot directory when devtools is active.

        2.外部配置源:常用:Java属性文件YAML文件环境变量命令行参数

        3.配置文件查找位置

                1.classpath 根路径

                2.classpath 根路径下config目录

                3.jar包当前目录

                4.jar包当前目录的config目录

                5. /config子目录的直接子目录

        4.配置文件加载顺序

                1.当前jar包内部的application.properties和application.yml

                2.当前jar包内部的application-{profile}.properties 和 application-{profile}.yml

                3.引用的外部jar包的application.properties和application.yml

                4.引用的外部jar包的application-{profile}.properties 和 application-{profile}.yml

        5.指定环境优先,外部优先,后面的可以覆盖前面的同名配置项

3.自定义starter

        1.starter启动原理(引入starter --- xxxAutoConfiguration --- 容器中放入组件 ---- 绑定xxxProperties ---- 配置项)

                1.starter-pom引入 autoconfigurer 包

                2.autoconfigure包中配置使用 META-INF/spring.factoriesEnableAutoConfiguration 的值,使得项目启动加载指定的自动配置类

                3.编写自动配置类 xxxAutoConfiguration -> xxxxProperties

                        1.@Configuration

                        2.@Conditional

                        3.@EnableConfigurationProperties

                        4.@Bean

                        5.......

        2.自定义starter

                1.atguigu-hello-spring-boot-starter(启动器)

                2.atguigu-hello-spring-boot-starter-autoconfigure(自动配置包)

4.SpringBoot原理(Spring原理【Spring注解】、SpringMVC原理、自动配置原理、SpringBoot原理)

        1.SpringBoot启动过程

                1.创建 SpringApplication

                        1.保存一些信息。

                        2.判定当前应用的类型。ClassUtils。Servlet

                        3.bootstrappers:初始启动引导器(List<Bootstrapper>):去spring.factories文件中找 org.springframework.boot.Bootstrapper

                        4.找 ApplicationContextInitializer;去spring.factories找 ApplicationContextInitializer:List<ApplicationContextInitializer<?>> initializers

                        5.ApplicationListener ;应用监听器。spring.factories找 ApplicationListener:List<ApplicationListener<?>> listeners

                2.运行 SpringApplication

                        1.StopWatch

                        2.记录应用的启动时间

                        3.创建引导上下文(Context环境)createBootstrapContext():获取到所有之前的 bootstrappers 挨个执行 intitialize() 来完成对引导启动器上下文环境设置

                        4.让当前应用进入headless模式。java.awt.headless

                        5.获取所有 RunListener(运行监听器)【为了方便所有Listener进行事件感知】:getSpringFactoriesInstances 去spring.factoriesSpringApplicationRunListener

                        6.遍历 SpringApplicationRunListener 调用 starting 方法:​​​​​​​相当于通知所有感兴趣系统正在启动过程的人,项目正在 starting。

                        7.保存命令行参数;ApplicationArguments

                        8.准备环境 prepareEnvironment()

                                1.返回或者创建基础环境信息对象。StandardServletEnvironment

                                2.配置环境信息对象:读取所有的配置源的配置属性值

                                3.绑定环境信息

                                4.监听器调用 listener.environmentPrepared();通知所有的监听器当前环境准备完成

                        9.创建IOC容器(createApplicationContext())

                                1.根据项目类型(Servlet)创建容器,

                                2.当前会创建 AnnotationConfigServletWebServerApplicationContext

                        10.​​​​​​​准备ApplicationContext IOC容器的基本信息   prepareContext()

                                1.保存环境信息

                                2.IOC容器的后置处理流程。

                                3.应用初始化器;applyInitializers

                                        1.遍历所有的 ApplicationContextInitializer 。调用 initialize.。来对ioc容器进行初始化扩展功能

                                        2.遍历所有的 listener 调用 contextPrepared。EventPublishRunListenr;通知所有的监听器contextPrepared

                                4.所有的监听器 调用 contextLoaded。通知所有的监听器 contextLoaded

                        11.​​​​​​​刷新IOC容器。refreshContext:创建容器中的所有组件(Spring注解)

                        12.容器刷新完成后工作?afterRefresh

                        13.所有监听 器 调用 listeners.started(context); 通知所有的监听器 started

                        14.调用所有runners;callRunners()

                                1.​​​​​​​获取容器中的 ApplicationRunner

                                2.获取容器中的 CommandLineRunner

                                3.合并所有runner并且按照@Order进行排序

                                4.遍历所有的runner。调用 run 方法

                        15.如果以上有异常:​​​​​​​调用Listener 的 failed

                        16.​​​​​​​调用所有监听器的 running 方法 listeners.running(context); 通知所有的监听器 running

                        17.running如果有问题。继续通知 failed 。调用所有 Listener 的 failed;通知所有的监听器 failed

public interface Bootstrapper {

	/**
	 * Initialize the given {@link BootstrapRegistry} with any required registrations.
	 * @param registry the registry to initialize
	 */
	void intitialize(BootstrapRegistry registry);

}
@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;

}

        2.Application Events and Listeners

                1.Core Features

                2.ApplicationContextInitializer

                3.ApplicationListener

                4.SpringApplicationRunListener

        3.ApplicationRunner 与 CommandLineRunner

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值