深入理解@SpringBootApplication 顶 原
欢迎来到阿八个人博客网站。本 阿八个人博客 网站提供最新的站长新闻,各种互联网资讯。 喜欢本站的朋友可以收藏本站,或者加QQ:我们大家一起来交流技术! URL链接:https://www.abboke.com/jsh/2019/0628/4150.html
>>>
1 官方文档
参见:18. Using the @SpringBootApplication Annotation
Many Spring Boot developers like their apps to use auto-configuration, component scan and be able to define extra configuration on their "application class". A single @SpringBootApplication annotation can be used to enable those three features, that is:
- @EnableAutoConfiguration: enable Spring Boot’s auto-configuration mechanism
- @ComponentScan: enable @Component scan on the package where the application is located (see the best practices)
- @Configuration: allow to register extra beans in the context or import additional configuration classes
The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan with their default attributes.
2 翻译
许多Spring Boot开发者们喜欢在ta们的应用程序中使用自动装配,组件扫描,和能够在ta们的“应用程序类”上定义额外的配置。 单个
@SpringBootApplication
注释可用于替代这三个功能,即
-
@EnableAutoConfiguration
: 启用Spring Boot的自动配置机制 -
@ComponentScan
: 在应用程序所在的包上启用@Component
扫描(请参阅最佳实践) -
@Configuration
: 允许在上下文中注册额外的bean或导入其他配置类
@SpringBootApplication
注解等同于使用@Configuration
,@EnableAutoConfiguration
和@ComponentScan
及其默认属性。
3 源码分析 version:2.0.2.RELEASE
3.1 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 = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {...}
接着看看@SpringBootConfiguration
和@Configuration
的源码:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration { }
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {...}
据此,能看出一点什么吗? 是的,@Configuration
注解上标注了@Component
,@Configuration
实际上是@Component
的"派生"注解。同样的道理,@SpringBootApplication
标注了@SpringBootConfiguration
。
所以,我们得到三者的关系:
-
@Component
-
@Configuration
- @SpringBootApplication
-
@Configuration
对的,上面三者具有层级关系。小马哥的书中称之为多层次@Component'派生性'
,这种能力允许开发人员扩展使用。
再回到@SpringBootApplication
,标注了@ComponentScan
,表明可以扫描到@Component
,刚才我们已经分析了,@SpringBootConfiguration
属于多层次的@Component
"派生"注解,属于能够被@ComponentScan
识别。
以此类推,@Repository
、@Service
和@Controller
均属于@Component
"派生"注解,不过它们是直接"派生",官方称之为"Spring模式注解"。
3.2 SpringBootApplication属性别名
首先看一个在项目中常用的属性使用:
@SpringBootApplication(scanBasePackages = "com.xxx.yyy.zzz")
public class AppApplication {...}
前面我们翻译了官方的说明,@SpringBootApplication
可以等同于@EnableAutoConfiguration
和@ComponentScan
,那么原理是什么呢?
我们先介绍一个新的注解@AliasFor
,同样地,先看看官方文档的介绍:
@AliasFor
is an annotation that is used to declare aliases for annotation attributes.
翻译过来:
@AliasFor
是用于声明注解属性的别名注解。
我的理解就是,通过@AliasFor
,一个注解可以把另外一个注解的属性以别名的方式加到注解的属性上面。下面我们再看看@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 {};
看完这段源码,是不是有种豁然开朗的感觉,@SpringBootApplication
可以等同于@EnableAutoConfiguration
和@ComponentScan
其实就是通过@AliasFor
,把另外两个注解的属性通过别名的方式"聚合"到了自己的身上。类似的注解还有@RestController
等。