@SpringBootApplication解析

我们经常写的@SpringBootApplication 是什么意思呢?*
@SpringBootApplication 源码点开瞟一眼:
fgf

》我们发现@SpringBootApplication 中包含
@SpringBootConfiguration、@ EnableAutoConfiguration、@ ComponentScan ,这一个注解代替了这三个注解; *

  • @Configuration 和 @bean 注入定义了一个实体类;
  • @EnableAutoConfiguration 启用了上下文自动配置;
  • @ComponentScan 扫描指定的包,若未指定值,默认扫包范围:被注释的 class所在的包;*

》1、继续查看@SpringBootConfiguration
sdfd

@Configuration注解

  • 声明了这是一个配置类,而springboot 会通过@ComponentScan 扫描添加
    带有@Configuration注解的配置类,并读取配置类的信息;

@SpringBootConfiguration注解

  • 是作用于 springboot应用的,@SpringBootConfiguration中可以添加N个@Configuration注解的配置类,但是对于一个springboot项目,只能存在一个@SpringBootConfiguration注解;

》2、查看@EnableAutoConfiguration
注解

2.1、能完成自动配置,主要是
@Inherited注解、@ConditionalOnMissingBean注解等

  • @ConditionalOnMissingBean注解生效,其上有@Conditional注解;
  • @EnableAutoConfiguration注解上存在@Import(AutoConfigurationImportSelector.class)自动导配置类的包,可以把所有带有@Configuration的类的包导入;

附上一段注释,说只要依靠@ConditionalOnMissingBean注解生效自动配置;
当missing 某一个bean时候,满足自动配置的条件,@EnableAutoConfiguration 会生效;

Auto-configuration classes are regular Spring {@link Configuration} beans. They are
 * located using the {@link SpringFactoriesLoader} mechanism (keyed against this class).
 * Generally auto-configuration beans are {@link Conditional @Conditional} beans (most
 * often using {@link ConditionalOnClass @ConditionalOnClass} and
 * {@link ConditionalOnMissingBean @ConditionalOnMissingBean} annotations).

2.2、跟进@ConditionalOnMissingBean
@ConditionalOnMissingBean中有@Conditional注解,该注解意思是条件匹配;
若满足@Conditional要求的条件,才会被注册到组件中;当我们自定义一些配置时候,就会默认配置就会失效;因为这个bean 已经存在了;

在这里插入图片描述

2.3、
@Inherited注解

sdgdgh

》会通过注解超类来找依赖中的类,完成自动初始化类加载

* Indicates that an annotation type is automatically inherited.  If
 * an Inherited meta-annotation is present on an annotation type
 * declaration, and the user queries the annotation type on a class
 * declaration, and the class declaration has no annotation for this type,
 * then the class's superclass will automatically be queried for the
 * annotation type.  This process will be repeated until an annotation for this
 * type is found, or the top of the class hierarchy (Object)
 * is reached.  If no superclass has an annotation for this type, then
 * the query will indicate that the class in question has no such annotation.
 *
 * <p>Note that this meta-annotation type has no effect if the annotated
 * type is used to annotate anything other than a class.  Note also
 * that this meta-annotation only causes annotations to be inherited
 * from superclasses; annotations on implemented interfaces have no
 * effect.

》3、@ComponentScan如何扫包的
56

》贴上一段注释,第二段意思是,如没有指明要扫的包,会从当前包路径向下扫包;
所以,main函数放在顶层;

 * Configures component scanning directives for use with @{@link Configuration} classes.
 * Provides support parallel with Spring XML's {@code <context:component-scan>} element.
 *
 * <p>Either {@link #basePackageClasses} or {@link #basePackages} (or its alias
 * {@link #value}) may be specified to define specific packages to scan. If specific
 * packages are not defined, scanning will occur from the package of the
 * class that declares this annotation.

》小结:
@SpringBootApplication 中包含
@SpringBootConfiguration、@ EnableAutoConfiguration、@ ComponentScan

@ EnableAutoConfiguration

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值