SpringBoot项⽬启动过程
正⽂
说springboot的启动流程当然少不了springboot启动⼊⼝类
@SpringBootApplication
public class SpringBootWebApplication {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(SpringBootWebApplication.class);
application.run(args);
}
}
以上代码很容易看出哪些是关键,当然是@SpringBootApplication和application.run()分别是springboot加载配置和启动,下⾯详细说明这两块。
1.@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 {
}
其中@Configuration(@SpringBootConfiguration中其实⽤的也是@Configuration);
@EnableAutoConfiguration;
@ComponentScan三个是最重要的注解,@SpringBootApplication整合了三个注解使⽤者写起来看起来都⽐较简洁。
1.1@Configuration
它就是JavaConfig形式的Spring Ioc容器的配置类使⽤的那个@Configuration,这⾥的启动类标注了@Configuration之后,本⾝其实也是⼀个IoC容器的配置类。
如下案例说明xml和注解实现bean的定义
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/schema/beans /schema/beans/spring-beans-3.0.xsd"
default-lazy-init="true">
<!--bean定义-->
</beans>
1.2@EnableAutoConfiguration
@SuppressWarnings("deprecation")
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
@EnableAutoConfiguration简单的说它的作⽤就是借助@Import的⽀持,收集和注册特定场景相关的bean定义。其中,最关键的要属
@Import(EnableAutoConfigurationImportSelector.class),借助EnableAutoConfigurationImportSelector,@EnableAutoConfiguration可以帮助SpringBoot应⽤将所有符合条件的@Configuration配置都加载到当前SpringBoot创建并使⽤的IoC容器。借助于Spring框架原有的⼀个⼯具类:SpringFactoriesLoader的⽀持很智能的⾃动配置:
SpringFactoriesLoader其主要功能就是从指定的配置⽂件META-INF/spring.factories加载配置。将其中
org.springframework.boot.autoconfigure.EnableAutoConfiguration对应的配置项通过反射(Java Refletion)实例化为对应的标注了
@Configuration的JavaConfig形式的IoC容器配置类,然后汇总为⼀个并加载到IoC容器。(如下页⾯模版的配置)
1.3springboot启动简单流程
1.3.1 当我们运⾏SpringApplication的main⽅法时,调⽤静态⽅法run()⾸先是实例化,SpringApplication初始化的时候主要做主要做三件事:
根据classpath下是否存在(ConfigurableWebApplicationContext)判断是否要启动⼀个web applicationContext。
SpringFactoriesInstances加载classpath下所有可⽤的ApplicationContextInitializer
SpringFactoriesInstances加载classpath下所有可⽤的ApplicationListener
1.3.2 SpringApplication实例化完成并且完成配置后调⽤run()⽅法,⾸先遍历初始化过程中加载的SpringApplicationRunListeners,然后调⽤starting(),开始监听springApplication的启动。spring ioc注解
1.3.3 加载SpringBoot配置环境(ConfigurableEnvironment),如果是通过web容器发布,会加载StandardEnvironment。将配置环境(Environment)加⼊到对象中(SpringApplicationRunListeners)。
1.3.4 启动banner属性的设置。
1.3.5 ConfigurableApplicationContext(应⽤配置上下⽂)创建,根据webEnvironment是否是web环境创建默认的contextClass
,AnnotationConfigEmbeddedWebApplicationContext(通过扫描所有注解类来加载bean)和ConfigurableWebApplicationContext),最后通过BeanUtils实例化上下⽂对象,并返回。
1.3.6 prepareContext()⽅法将listeners、environment、applicationArguments、banner等重要组件与上下⽂对象关联。
1.3.7 refreshContext(context),bean的实例化完成IoC容器可⽤的最后⼀道⼯序。
1.3.8 最后springboot做⼀些收尾⼯作。⾃此springboot的简单流程到此结束。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。