深入理解@SpringBootApplication

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

对的,上面三者具有层级关系。小马哥的书中称之为多层次@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等。

转载于:https://my.oschina.net/javamaster/blog/3067039

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值