摘要:从前面创建的Spring Boot应用示例中可以看到,启动一个Spring Boot工程都是从SpringApplication.run方法开始的。这个方法具体完成了哪些工作?@Spring-BootApplication注解的作用是什么?在本节内容中将找到答
从前面创建的Spring Boot应用示例中可以看到,启动一个Spring Boot工程都是从SpringApplication.run方法开始的。这个方法具体完成了哪些工作?@Spring-BootApplication注解的作用是什么?在本节内容中将找到答案。
通过查看SpringApplication.run方法的源码可以看到,该方法首先生成Spring-Application的一个实例,然后调用实例的run方法。下面来看一下SpringApplication构造函数的源码:
public SpringApplication(ResourceLoader resourceLoader,
Class...
primarySources) {
this.sources = new LinkedHashSet;
this.bannerMode = Mode.CONSOLE;
this.logStartupInfo = true;
//①
this.addCommandLineProperties = true;
this.addConversionService = true;
this.headless = true;
this.registerShutdownHook = true;
this.additionalProFiles = new HashSet;
this.isCustomEnvironment = false;
this.lazyInitialization = false;
//②
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must
not be null");
this.primarySources = new LinkedHashSet(Arrays.asList
(primarySources));
this.webApplicationType =
WebApplicationType.deduceFrom
Classpath;
//③
this.setInitializers(this.getSpringFactoriesInstances(Applica
tion
ContextInitializer.class));
//④
this.setListeners(this.getSpringFactoriesInstances
(ApplicationListener.class));
//⑤
this.mainApplicationClass =
this.deduceMainApplicationClass;
}
注释①:打印启动信息。
注释②:表示Bean是否要以懒加载的方式进行实例化。
注释③:初始化WebApplicationType的类型,主要包括REACTIVE、SERVLET和NONE三种类型。
注释④:加载ApplicationContextInitializer类。
注释⑤:加载ApplicationListener类。
注释④和⑤是加载META-INF/spring.factories文件中配置的ApplicationContext-Initializer和ApplicationListener类。具体配置代码如下:
org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.context.ConfigurationWarningsApplica
tion
ContextInitializer,\
org.springframework.boot.context.ContextIdApplicationContextI
nitializer,\
org.springframework.boot.context.config.DelegatingApplication
ContextInitializer,\
org.springframework.boot.rsocket.context.RSocketPortInfoAppli
cation
ContextInitializer,\
org.springframework.boot.web.context.ServerPortInfoApplicatio
n
ContextInitializer
org.springframework.context.ApplicationListener=\
org.springframework.boot.ClearCachesApplicationListener,\
org.springframework.boot.builder.ParentContextCloserApplicati
onListener,\
org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPos
t
Processor,\
org.springframework.boot.context.FileEncodingApplicationListe
ner,\
org.springframework.boot.context.config.AnsiOutputApplication
Listener,\
org.springframework.boot.context.config.ConfigFileApplication
Listener,\
Listener,\
org.springframework.boot.context.logging.ClasspathLoggingAppl
ication
Listener,\
org.springframework.boot.context.logging.LoggingApplicationLi
stener,\
org.springframework.boot.liquibase.LiquibaseServiceLocatorApp
lication
Listener
通过上面的构造函数可以看到,SpringApplication类的主要的工作是确定Web应用类型、加载上下文初始化器及监听器等。
接下来看重要的部分,即SpringApplication实例的run方法,具体源代码如下:
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch;
stopWatch.start;
ConfigurableApplicationContext context = null;
Collection
exceptionReporters =
new ArrayList;
this.configureHeadlessProperty;
SpringApplicationRunListeners listeners =
this.getRunListeners
(args);
//①
listeners.starting;
Collection exceptionReporters;
try {
ApplicationArguments applicationArguments = new
Default
ApplicationArguments(args);
ConfigurableEnvironment environment =
this.prepareEnvironment
(listeners, applicationArguments);
//②
this.configureIgnoreBeanInfo(environment);
Banner printedBanner =
this.printBanner(environment);
context = this.createApplicationContext;
//③
exceptionReporters =
this.getSpringFactoriesInstances
(SpringBootExceptionReporter.class, new Class
{ConfigurableApplication
Context.class}, context);
this.prepareContext(context, environment,
listeners,
applicationArguments, printedBanner);
//④
this.refreshContext(context);
//⑤
this.afterRefresh(context, applicationArguments);
stopWatch.stop;
if (this.logStartupInfo) {
(new
StartupInfoLogger(this.mainApplicationClass)).
logStarted(this.getApplicationLog, stopWatch);
}
listeners.started(context);
//⑥
this.callRunners(context, applicationArguments);
//⑦
} catch (Throwable var10) {
this.handleRunFailure(context, var10,
exceptionReporters,
listeners);
throw new IllegalStateException(var10);
}
try {
listeners.running(context);
//⑧
return context;
} catch (Throwable var9) {
this.handleRunFailure(context, var9,
exceptionReporters,
(SpringApplicationRunListeners)null);
throw new IllegalStateException(var9);
}
}
注释①:初始化监听器,并开启监听器进行事件监听。
注释②:准备上下文环境,包括运行机器的环境变量、应用程序启动的变量和配置文件的变量等。初始化上下文环境后,启动监听器listeners.environment-Prepared(environment)。
注释③:初始化应用上下文。根据WebApplicationType类型的不同,生成的上下文也不同。如果是SERVLET,则对应生成AnnotationConfigServletWebServer-ApplicationContext;如果是REACTIVE,则对应生成AnnotationConfigReactiveWebServerApplicationContext。默认生成AnnotationConfigApplicationContext。
注释④:刷新应用上下文的准备工作。此处主要用于设置容器环境,启动监听器listeners.contextPrepared(context),加载启动类,并调用listeners.contextLoaded (context)方法。
注释⑤:刷新应用上下文,主要用于进行自动化装配和初始化IoC容器。
注释⑥:容器启动事件。
注释⑦:如果有用户定义的CommandLineRunner或者
ApplicationRunner,则遍历执行它们。
注释⑧:容器运行事件。
通过分析源代码,总结出Spring Boot启动的主要流程如图3.4所示。
图3.4 Spring Boot的启动流程
通过分析源代码还可以发现,事件监听是Spring框架中重要的一部分。Spring提供了多种类型的事件,常用的如表3.1所示。
在Spring Boot的入口类中,有一个重要的注解@SpringBootApplication,本节将分析该注解的作用。首先查看@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 =
AutoConfiguration
ExcludeFilter.class) })
public @interface SpringBootApplication {
@AliasFor(annotation = EnableAutoConfiguration.class)
Class exclude default {};
@AliasFor(annotation = ComponentScan.class, attribute
= "base
Packages")
String scanBasePackages default {};
= "base
PackageClasses")
Class scanBasePackageClasses default {};
@AliasFor(annotation = Configuration.class)
boolean proxyBeanMethods default true;
}
通过源码可以看到,@SpringBootApplication是一个复合注解,包括@Component-Scan、@EnableAutoConfiguration和@SpringBootConfiguration等。下面具体分析这3个注解。
1. @ComponentScan注解
在第1章中讲过Bean的注解,如@Service、@Repository、@Component和@Controller等。@ComponentScan注解可以自动扫描被以上注解描述的Bean并将其加载到IoC容器中。@ComponentScan注解还有许多属性,通过这些属性可以更准确地指定哪些Bean被扫描并注入。
basePackages:指定需要扫描的包路径。
basePackageClasses:指定类,并扫描该类所在包下的所有组件。
includeFilters:指定哪些类可以通过扫描。
excludeFilters:指定哪些类不被扫描。
lazyInit:指定扫描的对象是否要懒加载。
resourcePattern:指定符合条件的类文件。
2. @EnableAutoConfiguration注解
@EnableAutoConfiguration注解是Spring Boot实现自动化配置加载的核心注解。通过@Import注入一个名为AutoConfigurationImportSelector的类,Spring-FactoriesLoader类加载类路径下的META-INF/spring.factories文件来实现自动配置加载的过程。其中,spring.factories文件配置了org.springframework.boot.autoconfigure.EnableAutoConfiguration属性值,可以加载配置了@Configuration注解的类到IoC容器中。
@SpringBootConfiguration注解的功能类似于@Configuration注解,声明当前类是一个配置类,它会将当前类中有@Bean注解的实例加载到IoC容器中。
来源:大数据架构师