入口
@springbootapplication
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication
- @SpringBootConfiguration(在这个类的源码中又有一个Configuration的注解)
@Configuration这个注解的作用就是声明当前类是一个配置类,然后Spring会自动扫描到添加了@Configuration的类,读取其中的配置信息,而@SpringBootConfiguration是来声明当前类是SpringBoot应用的配置类,项目中只能有一个。所以一般我们无需自己添加。 - @EnableAutoConfiguration
开启自动配置,告诉SpringBoot基于所添加的依赖,去“猜测”你想要如何配置Spring。比如我们引入了spring-boot-starter-web,而这个启动器中帮我们添加了tomcat、SpringMVC的依赖,此时自动配置就知道你是要开发一个web应用,所以就帮你完成了web及SpringMVC的默认配置了!我们使用SpringBoot构建一个项目,只需要引入所需框架的依赖,配置就可以交给SpringBoot处理了。 - @ComponentScan
配置组件扫描的指令
提供了类似与context:component-scan标签的作用.通过basePackageClasses或者basePackages属性来指定要扫描的包。如果没有指定这些属性,那么将从声明这个注解的类所在的包开始,扫描包及子包.而我们的@SpringBootApplication注解声明的类就是main函数所在的启动类,因此扫描的包是该类所在包及其子包。因此,一般启动类会放在一个比较前的包目录中
@value
@configuration
@propertySource
@ConfigurationProperties
@ConfigurationProperties是springboot中注解,用于将主配置文件(application.properties或者“application.yml” )中的属性,映射到实体类中对应的属性。
意思就是把主配置文件中配置属性设置到对应的Bean属性上。
常见使用方式:
@ConfigurationProperties + @Component注解到bean定义类上
@ConfigurationProperties + @Bean注解在配置类的bean定义方法上
@ConfigurationProperties注解到普通类然后通过 @EnableConfigurationProperties定义为bean
@PropertySource 是spring的注解
加载指定的属性文件的配置到 Spring 的 Environment 中。可以配合 @Value 和 @ConfigurationProperties 使用。
用法:
@Configuration+ @PropertySource+Environment
@Configuration+ @PropertySource+@Value
@Configuration+ @PropertySource+@ConfigurationProperties
@PropertySource指定加载哪个文件,@ConfigurationProperties指定加载文件中的哪一类属性。
@PropertySource+@ConfigurationProperties在一起解决了@ConfigurationProperties只能加载主文件内属性问题。
@Enable* 注解
@EnableConfigurationProperties
@EnableAsync @Async
原理 @Import(), 导入类到 spring中 ,用来导入一个或多个类 (spring托管),或者配置类(配置类里的所有bean都会被spring托管 )。
@import(Importselect.class) Importselect 返回值必须是一个class,该class会被spring容器托管。
@import(ImportBeanDefinitionRegistrar.class)
@EnableAutoConfiguration
关键点有两个:
1:ImportSelector 该接口方法返回值都会被纳入到spring容器中
2:SpringFactoriesLoader 读取所有 META-INF/factories ,并读取配置
事件
- 自定义事件,继承ApplicationEvent抽象类
- 定义事件监听器,实现ApplicationListenter接口
- 配置监听器,监听器加入spring容器
- 发布事件,使用ApplicationContext.publishEvent
配置监听器:
1.SpringApplication.addListeners 添加监听器
2.@component 加入spring容器
3.使用 context.listener.classes 配置项配置。
4.@EventLinstener
拓展点
-
ApplicationContextInitializer
refresh 之前回调。
实现 接口,再注册 (SpringApplication.addInitializers或者 context.Initializer.classes 配置项配置) -
Aware系列
BeanNameAware
ApplicationContextAware
BeanFactoryAware -
BeanPostProcessor系列s
BeanPostProcessor
BeanFactoryPostProcessor
InstantiationAwareBeanPostProcessor
MergedBeanDefinitionPostProcessor -
初始化和销毁
InitialingBean和@PostConstruct,initMethod
DisposableBean和@PreDestroy,destroyMethod -
其他
FactoryBean -
CommandLineRunner
启动成功后最后一步的回调 (开机自启动)
实现在应用启动后,去执行相关代码逻辑,且只会执行一次;
spring batch批量处理框架依赖这些执行器去触发执行任务;
我们可以在run()方法里使用任何依赖,因为它们已经初始化好了;
我们需要使用CommandLineRunner或ApplicationRunner接口创建bean,spring boot会自动监测到它们。这两个接口都有一个run()方法,在实现接口时需要覆盖该方法,并使用@Component注解使其成为bean。
CommandLineRunner和ApplicationRunner的作用是相同的。不同之处在于CommandLineRunner接口的run()方法接收String数组作为参数,即是最原始的参数,没有做任何处理;而ApplicationRunner接口的run()方法接收ApplicationArguments对象作为参数,是对原始参数做了进一步的封装。
当程序启动时,我们传给main()方法的参数可以被实现CommandLineRunner和ApplicationRunner接口的类的run()方法访问,即可接收启动服务时传过来的参数。我们可以创建多个实现CommandLineRunner和ApplicationRunner接口的类。为了使他们按一定顺序执行,可以使用@Order注解或实现Ordered接口。
流程
/**
* 创建一个SpringApplication实体,应用程序上下文将从指定的主源文档加载bean以获取详细信息,
* 这个实例可以在调用之前自定义
* @param resourceLoader
* @param primarySources
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
//使用的资源加载器
this.resourceLoader = resourceLoader;
//主要的bean资源 primarySources【在这里是启动类所在的.class】,不能为null,如果为null,抛异常
Assert.notNull(primarySources, "PrimarySources must not be null");
//启动类的实例数组转化成list,放在LinkedHashSet集合中
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
/**
* 创建应用类型,不同应用程序类型,创建不同的环境
* springboot1.5 只有两种类型:web环境和非web环境
* springboot2.0 有三种应用类型:WebApplicationType
* NONE:不需要再web容器的环境下运行,也就是普通的工程
* SERVLET:基于servlet的Web项目
* REACTIVE:响应式web应用reactive web Spring5版本的新特性
*/
this.webApplicationType = WebApplicationType.deduceFromClasspath();
/**
* 每一个initailizer都是一个实现了ApplicationContextInitializer接口的实例。
* ApplicationContextInitializer是Spring IOC容器中提供的一个接口: void initialize(C applicationContext);
* 这个方法它会在ConfigurableApplicationContext的refresh()方法调用之前被调用(prepareContext方法中调用),
* 做一些容器的初始化工作。
*/
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
/**
* Springboot整个生命周期在完成一个阶段的时候都会通过事件推送器(EventPublishingRunListener)产生一个事件(ApplicationEvent),
* 然后再遍历每个监听器(ApplicationListener)以匹配事件对象,这是一种典型的观察者设计模式的实现
* 具体事件推送原理请看:sb事件推送机制图
*/
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
// 指定main函数启动所在的类,即启动类BootApplication.class
this.mainApplicationClass = deduceMainApplicationClass();
}
/**
* 运行应用程序,创建并刷新一个新的应用程序上下文
*
* @param args
* @return
*/
public ConfigurableApplicationContext run(String... args) {
/**
* StopWatch: 简单的秒表,允许定时的一些任务,公开每个指定任务的总运行时间和运行时间。
* 这个对象的设计不是线程安全的,没有使用同步。SpringApplication是在单线程环境下,使用安全。
*/
StopWatch stopWatch = new StopWatch();
// 设置当前启动的时间为系统时间startTimeMillis = System.currentTimeMillis();
stopWatch.start();
// 创建一个应用上下文引用
ConfigurableApplicationContext context = null;
// 异常收集,报告启动异常
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
/**
* 系统设置headless模式(一种缺乏显示设备、键盘或鼠标的环境下,比如服务器),
* 通过属性:java.awt.headless=true控制
*/
configureHeadlessProperty();
/*
* 获取事件推送监器,负责产生事件,并调用支某类持事件的监听器
* 事件推送原理看上面的事件推送原理图
*/
SpringApplicationRunListeners listeners = getRunListeners(args);
/**
* 发布一个启动事件(ApplicationStartingEvent),通过上述方法调用支持此事件的监听器
*/
listeners.starting();
try {
// 提供对用于运行SpringApplication的参数的访问。取默认实现
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
/**
* 构建容器环境,这里加载配置文件
*/
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
// 对环境中一些bean忽略配置
configureIgnoreBeanInfo(environment);
// 日志控制台打印设置
Banner printedBanner = printBanner(environment);
// 创建容器
context = createApplicationContext();
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[] { ConfigurableApplicationContext.class }, context);
/**
* 准备应用程序上下文
* 追踪源码prepareContext()进去我们可以发现容器准备阶段做了下面的事情:
* 容器设置配置环境,并且监听容器,初始化容器,记录启动日志,
* 将给定的singleton对象添加到此工厂的singleton缓存中。
* 将bean加载到应用程序上下文中。
*/
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
/**
* 刷新上下文
* 1、同步刷新,对上下文的bean工厂包括子类的刷新准备使用,初始化此上下文的消息源,注册拦截bean的处理器,检查侦听器bean并注册它们,实例化所有剩余的(非延迟-init)单例。
* 2、异步开启一个同步线程去时时监控容器是否被关闭,当关闭此应用程序上下文,销毁其bean工厂中的所有bean。
* 。。。底层调refresh方法代码量较多
*/
refreshContext(context);
afterRefresh(context, applicationArguments);
// stopwatch 的作用就是记录启动消耗的时间,和开始启动的时间等信息记录下来
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
}
// 发布一个已启动的事件
listeners.started(context);
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
// 发布一个运行中的事件
listeners.running(context);
}
catch (Throwable ex) {
// 启动异常,里面会发布一个失败的事件
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
return context;
}