#spring boot 相关知识
MANIFEST.MF 文件
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: jin
Start-Class: com.nbd.ocp.study.springboot.OcpStudySpringBootApplication
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Spring-Boot-Version: 2.1.3.RELEASE
Created-By: Apache Maven 3.5.4
Build-Jdk: 1.8.0_202
Main-Class: org.springframework.boot.loader.JarLauncher
spring boot基础加载类:
org.springframework.boot.loader.Launcher
org.springframework.boot.loader.ExecutableArchiveLauncher:基于jar
org.springframework.boot.loader.archive.JarFileArchive :基于文件系统
org.springframework.boot.loader.JarLauncher
org.springframework.boot.loader.WarLauncher
spring boot 加载顺序
以下拆分较细,spring定义的阶段详见org.springframework.boot.SpringApplicationRunListener
- 入口:Main-Class:(org.springframework.boot.loader.JarLauncher/WarLauncher)
- org.springframework.boot.loader.JarLauncher.main() launch(args) //开始加载
- org.springframework.boot.loader.Launcher.launch() JarFile.registerUrlProtocolHandler();//在系统属性中设置注册了自定义的URL处理器
- org.springframework.boot.loader.Launcher.launch() ClassLoader classLoader = createClassLoader(getClassPathArchives());//Spring-Boot-* 创建类加载器
- org.springframework.boot.loader.Launcher.launch() launch(args, getMainClass(), classLoader);//Start-Class 执行初始化逻辑
- 执行Start-Class对应的main函数
- org.springframework.boot.SpringApplication.run(Class<?> primarySource, String… args):
- new SpringApplication(primarySources): 初始化包信息,
- org.springframework.boot.SpringApplication.run(String… args)
- 启动时间控制,记录启动时间:StopWatch stopWatch = new StopWatch();
- 初始化监听器:SpringApplicationRunListeners listeners = this.getRunListeners(args);
- 发布ApplicationStartedEvent事件:listeners.starting();
- 装配参数和环境:ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
- 打印banner:Banner printedBanner = this.printBanner(environment);
- 创建ApplicationContext:context = this.createApplicationContext();
- 装配Context:this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
- 刷新上下文:this.refreshContext(context);
- 刷新后:this.afterRefresh(context, applicationArguments);
- 调用所有监听器的started(ctx):listeners.started(context);
- 调用实现了 ApplicationRunner、CommandLineRunner 的对象的 run 方法:callRunners(context, applicationArguments);
- 调用所有监听器的running():listeners.running(context);
- 返回上下文
spring boot 加载默认实现
详见:org.springframework.boot.spring-boot包内的spring.factories。各个实现的作用详见类注解。
# PropertySource Loaders
org.springframework.boot.env.PropertySourceLoader=\
org.springframework.boot.env.PropertiesPropertySourceLoader,\
org.springframework.boot.env.YamlPropertySourceLoader
# Run Listeners
org.springframework.boot.SpringApplicationRunListener=\
org.springframework.boot.context.event.EventPublishingRunListener
# Application Context Initializers
org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,\
org.springframework.boot.context.ContextIdApplicationContextInitializer,\
org.springframework.boot.context.config.DelegatingApplicationContextInitializer,\
org.springframework.boot.context.web.ServerPortInfoApplicationContextInitializer
# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.builder.ParentContextCloserApplicationListener,\
org.springframework.boot.context.FileEncodingApplicationListener,\
org.springframework.boot.context.config.AnsiOutputApplicationListener,\
org.springframework.boot.context.config.ConfigFileApplicationListener,\
org.springframework.boot.context.config.DelegatingApplicationListener,\
org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener,\
org.springframework.boot.logging.ClasspathLoggingApplicationListener,\
org.springframework.boot.logging.LoggingApplicationListener
# Environment Post Processors
org.springframework.boot.env.EnvironmentPostProcessor=\
org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor,\
org.springframework.boot.env.SpringApplicationJsonEnvironmentPostProcessor
spring boot扩展点
ApplicationContextInitializer
说明
主要用来在ConfigurableApplicationContext#refresh()之前对ConfigurableApplicationContext进行一些初始化的操作。 一般来说主要做一些程序化的操作,比如注册配置源,根据配置激活某个profile等
* org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer:检查一些错误的configuration
* org.springframework.boot.context.ContextIdApplicationContextInitializer:主要用来设置contextId
* org.springframework.boot.context.config.DelegatingApplicationContextInitializer:主要用来委派执行ConfigurableEnvironment中定义的那些ApplicationContextInitializer
* org.springframework.boot.context.web.ServerPortInfoApplicationContextInitializer:设置server的端口至Environment properties
自定义
一、 添加实现类
添加类实现ApplicationContextInitializer接口
public class MyApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
System.out.println("=======“);
}
}
二、注册到spring boot
方法1:
@SpringBootApplication
public class BlogApplication {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(BlogApplication.class);
//关键一步:将一个或多个initializer加入至spring容器中
springApplication.addInitializers(new MyApplicationContextInitializer());
springApplication.run(args).close();
}
}
方法2:
在application.properties中以context.initializer.classes为key配置一个或多个自定义Initializer
context.initializer.classes=com.MyApplicationContextInitializer
SpringApplicationRunListener
SpringApplication.run()的生命周期广播各个事件,实现类如:EventPublishingRunListener。此监听可以监听的时间详见接口:org.springframework.boot.SpringApplicationRunListener
使用:
* 创建CustomSpringApplicationRunListener继承org.springframework.boot.SpringApplicationRunListener。构造方法需要两个参数:org.springframework.boot.SpringApplication,String[]
* META-INF/spring.fatories 内添加 org.springframework.boot.SpringApplicationRunListener=com.CustomSpringApplicationRunListener
ApplicationListener
接口ApplicationListener是基于观察者模式监听ApplicationEvent并触发指定的操作。参考以实现类即可。
CommandLineRunner
在容器初始化完成的最后回调.效果等同于@PostConstruct。不同之处为@PostConstruct为方法级,比CommandLineRunner更先执行。ApplicationListener功能更强大一些(可指定监听事件),获取的参数更丰富。
spring容器扩展点
ImportBeanDefinitionRegistrar
在Spring中动态注册bean的通用做法是实现ImportBeanDefinitionRegistrar接口。ImportBeanDefinitionRegistrar需要配合@Import注解,@Import注解导入实现了ImportBeanDefinitionRegistrar接口的类
场景:
- 自定义扫描规则。注,spring默认不会扫描带注解的接口。
- 动态添加代理类。
BeanFactoryPostProcessor
是在spring容器加载了bean的定义文件之后,在bean实例化之前执行的
BeanPostProcessors
是在spring容器加载了bean的定义文件并且实例化bean之后执行的。BeanPostProcessor的执行顺序是在BeanFactoryPostProcessor之后
初始化方法指:
- bean实现了InitializingBean接口,对应的方法为afterPropertiesSet
- 在bean定义的时候,通过init-method设置的方法