承接前文springboot情操陶冶-@Configuration注解解析,本文将在前文的基础上对@SpringBootApplication注解作下简单的分析

@SpringBootApplication

该注解是springboot最集中的一个注解,也是应用最广泛的注解。官方也多用此注解以启动spring服务,我们看下其中的源码

@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 { @AliasFor(annotation = EnableAutoConfiguration.class)
Class<?>[] exclude() default {}; @AliasFor(annotation = EnableAutoConfiguration.class)
String[] excludeName() default {}; @AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
String[] scanBasePackages() default {}; @AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
Class<?>[] scanBasePackageClasses() default {}; }

通过上述代码可知,其整合了@EnableAutoConfiguration@ComponentScan两个主要的注解,并以此作了默认的指定。

  1. 属性excludeexcludeName,其默认为空,指定的话最终由EnableAutoConfiguration.class来完成解析

  2. 属性scanBasePackagesscanBasePackageClasses,其默认为空,指定的话最终由ComponentScan.class来完成解析

  3. 默认情况下,以@SpringBootApplication注解的所在类的包名作为beanDefinition扫描路径。

    扫描过程中屏蔽META-INF\spring.factories文件中org.springframework.boot.autoconfigure.EnableAutoConfiguration属性指定的class集合

  4. 支持多个@ComponentScan注解同时与@SpringBootApplication注解搭配使用

@ComponentScan注解在前文中已了解的差不多了,笔者此处就针对@EnableAutoConfiguration注解作下详细的分析

@EnableAutoConfiguration

单纯从英文单词上看是开启自动注入的意思,具体的话笔者还是根据源码来解读。首先看下注解的源码

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration { String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration"; /**
* Exclude specific auto-configuration classes such that they will never be applied.
* @return the classes to exclude
*/
Class<?>[] exclude() default {}; /**
* Exclude specific auto-configuration class names such that they will never be
* applied.
* @return the class names to exclude
* @since 1.3.0
*/
String[] excludeName() default {}; }

它用到了我们前文提及的@Import注解,所以我们关注所引入的AutoConfigurationImportSelector.class

AutoConfigurationImportSelector

其是DeferredImportSelector.class接口的实现类,此处我们直接查看复写的方法selectImports()

	@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
// spring.boot.enableautoconfiguration属性读取,默认为true
if (!isEnabled(annotationMetadata)) {
return NO_IMPORTS;
}
// read all META-INF\spring-autoconfigure-metadata.properties files
AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader
.loadMetadata(this.beanClassLoader); // 获取相应类上@EnableAutoConfiguration对应的属性
AnnotationAttributes attributes = getAttributes(annotationMetadata); // read all <org.springframework.boot.autoconfigure.EnableAutoConfiguration> properties from META-INF\spring.factories
List<String> configurations = getCandidateConfigurations(annotationMetadata,
attributes);
configurations = removeDuplicates(configurations); // read exclude/excludeName property or spring.autoconfigure.exclude property
Set<String> exclusions = getExclusions(annotationMetadata, attributes); // make sure the assignable exclusions are present in classpath and in configurations collection, or will throw exception
checkExcludedClasses(configurations, exclusions);
configurations.removeAll(exclusions);
// filter the present configurations
configurations = filter(configurations, autoConfigurationMetadata);
// fire the AutoConfigurationImportEvent by AutoConfigurationImportListener.class
fireAutoConfigurationImportEvents(configurations, exclusions); return StringUtils.toStringArray(configurations);
}

对上述的步骤此处再作下总结

  1. 优先判断环境变量spring.boot.enableautoconfiguration,如果指定为false,则不引入任何类。默认为true

  2. 读取classpath环境下所有的META-INF\spring-autoconfigure-metadata.properties文件,加载其中的所有属性保存至autoConfigurationMetadata

  3. 获取相应类上@EnableAutoConfiguration对应的属性,其实也就是exclude属性和excludeName属性

  4. 读取classpath环境下所有的META-INF\spring.factories文件中的org.springframework.boot.autoconfigure.EnableAutoConfiguration属性,得到configurations集合

  5. 根据第三步读取的配置以及spring.autoconfigure.exclude环境变量指定的配置,得到exclusions集合

  6. 确保exclusions集合是configurations集合的子集,以及exclusions集合内的class均是存在于classpath环境的。否则异常会抛出

  7. 根据上述的exclusions集合筛选出未被过滤的configurations集合。

  8. 根据第7点筛选出来的configurations集合,再在autoConfigurationMetadata的基础上针对ConditionalOnClass属性筛选一轮

    比如:org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration.ConditionalOnClass=org.springframework.cache.CacheManager

    表示如果要应用CacheAutoConfiguration,则得保证org.springframework.cache.CacheManager类存在

  9. 触发AutoConfigurationImportEvent事件

  10. 返回筛选过后的configurations集合

笔者此处罗列下spring-boot-autoconfigure-2.0.3.REALEASE包中的spring.factories中的EnableAutoConfiguration默认属性,属性太多,节选如下

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
org.springframework.boot.autoconfigure.cloud.CloudAutoConfiguration,\
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration,\
...
org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.artemis.ArtemisAutoConfiguration,\
...
org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration,\
...
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.reactive.WebSocketReactiveAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketMessagingAutoConfiguration,\
org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration

当然用户也可以自定义去实现需要自动注入的配置类。

总结

@SpringBootApplication注解内含@EnableAutoConfiguration注解和@ComponentScan注解,所以这两个注解结合再搭配上@Configuration注解便可以实现springboot的相关功能了。

在这之中,@EnableAutoConfiguration注解最为重要,其扩展性很好,方便springboot无缝对接不同的插件(NoSql插件、Web插件、JMX协议插件等等),读者需要好好分析。

下节预告

通过查阅上述的多个自动注解,发现用到了@ConditionalAutoConfigureAfter注解,都属于条件判断,在深入理解springboot整合其他插件前,必须对此两个注解有深刻的理解。

springboot情操陶冶-@SpringBootApplication注解解析的更多相关文章

  1. springboot情操陶冶-@ConfigurationProperties注解解析

    承接前文springboot情操陶冶-@Configuration注解解析,本文将在前文的基础上阐述@ConfigurationProperties注解的使用 @ConfigurationProper ...

  2. springboot情操陶冶-@Configuration注解解析

    承接前文springboot情操陶冶-SpringApplication(二),本文将在前文的基础上分析下@Configuration注解是如何一步一步被解析的 @Configuration 如果要了 ...

  3. SpringMVC源码情操陶冶-AnnotationDrivenBeanDefinitionParser注解解析器

    mvc:annotation-driven节点的解析器,是springmvc的核心解析器 官方注释 Open Declaration org.springframework.web.servlet.c ...

  4. springboot情操陶冶-web配置(一)

    承接前文springboot情操陶冶-@SpringBootApplication注解解析,在前文讲解的基础上依次看下web方面的相关配置 环境包依赖 在pom.xml文件中引入web依赖,炒鸡简单, ...

  5. springboot情操陶冶-@Conditional和@AutoConfigureAfter注解解析

    承接前文springboot情操陶冶-@Configuration注解解析,本文将在前文的基础上阐述@AutoConfigureAfter和@Conditional注解的作用与解析 1.@Condit ...

  6. springboot情操陶冶-jmx解析

    承接前文springboot情操陶冶-@Configuration注解解析,近期笔者接触的项目中有使用到了jmx的协议框架,遂在前文的基础上讲解下springboot中是如何整合jmx的 知识储备 J ...

  7. springboot情操陶冶-SpringApplication(二)

    承接前文springboot情操陶冶-SpringApplication(一),本文将对run()方法作下详细的解析 SpringApplication#run() main函数经常调用的run()方 ...

  8. springboot情操陶冶-web配置(七)

    参数校验通常是OpenApi必做的操作,其会对不合法的输入做统一的校验以防止恶意的请求.本文则对参数校验这方面作下简单的分析 spring.factories 读者应该对此文件加以深刻的印象,很多sp ...

  9. springboot情操陶冶-web配置(四)

    承接前文springboot情操陶冶-web配置(三),本文将在DispatcherServlet应用的基础上谈下websocket的使用 websocket websocket的简单了解可见维基百科 ...

随机推荐

  1. Visual Studio学习记录

    1,一些快捷键记录 1,折叠 ctrl+M+A: 折叠所有代码[官方名:折叠所有大纲提示] ctrl + M + O:折叠全部代码[官方:折叠到定义],但是这个貌似只能折叠代码,xml之类的无效.m+ ...

  2. index.php入口文件至根目录

      登录|注册       咖啡如同生活的专栏 从不羡慕别人的优秀,因为相信自己也可以优秀. 闲下来时看看书,书本里的故事,总有你学到的人生.       目录视图 摘要视图 订阅 异步赠书:9月重磅 ...

  3. python基础自学 第二天

    注释 分类 单行注释 多行注释 作用 使用自己熟悉的语言,在程序中对某些代码进行标注说明,增强程序可读性 单行注释(行注释) 以 # 开头,#右边所有的东西就被当成说明文字,而不是要执行的程序,只是说 ...

  4. Android中自定义Preference

    一.需求 开发横屏设备的app时,发现preference显示的都是上下结构,因此需要自定义preference实现横屏显示. 二.layout实现 <?xml version="1. ...

  5. Python之旅Day15 Bootstrap与Django初识

    Bootstrap初识 Bootstrap简介 Bootstrap 是最受欢迎的 HTML.CSS 和 JS 框架,用于开发响应式布局.移动设备优先的 WEB 项目.(官网http://www.boo ...

  6. spring-data-redis和jedis版本对应收集总结

    基于spring主版本为4.3.13.RELEASE的项目,测试以下对应版本可用. spring-data-redis版本 jedis版本 备注 1.5.2.RELEASE 2.7.3   1.6.0 ...

  7. 架构(二)Maven安装以及Nexus配置

    一 Maven安装配置 1.1 下载 http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.5.4/binaries/apache-ma ...

  8. 00SQL表字段说明

    SELECT d.name 表名 , a.colorder 字段序号 , a.name 字段名 , ISNULL(g.[value], '') AS 字段说明 , ( CASE WHEN COLUMN ...

  9. JavaScript之DOM创建节点

    上几篇文章中我们罗列了一些获取HTML页面DOM对象的方法,当我们获取到了这些对象之后,下一步将对这些对象进行更改,在适当的时候进行对象各属性的修改就形成了我们平时看到的动态效果.具体js中可以修改D ...

  10. RichText 富文本开源项目总结

    在Android开发中,我们不免会遇到富文本的编辑和展示的需求,以下是本人之前star的富文本编辑器的开源项目,供大家参考: 一.RichEditor 开源项目地址:https://github.co ...