天天看點

@SpringBootApplication解析

我們經常寫的@SpringBootApplication 是什麼意思呢?*

@SpringBootApplication 源碼點開瞟一眼:

@SpringBootApplication解析

》我們發現@SpringBootApplication 中包含

@SpringBootConfiguration、@ EnableAutoConfiguration、@ ComponentScan ,這一個注解代替了這三個注解; *

  • @Configuration 和 @bean 注入定義了一個實體類;
  • @EnableAutoConfiguration 啟用了上下文自動配置;
  • @ComponentScan 掃描指定的包,若未指定值,預設掃包範圍:被注釋的 class所在的包;*

》1、繼續檢視@SpringBootConfiguration

@SpringBootApplication解析
@Configuration注解
  • 聲明了這是一個配置類,而springboot 會通過@ComponentScan 掃描添加

    帶有@Configuration注解的配置類,并讀取配置類的資訊;

@SpringBootConfiguration注解
  • 是作用于 springboot應用的,@SpringBootConfiguration中可以添加N個@Configuration注解的配置類,但是對于一個springboot項目,隻能存在一個@SpringBootConfiguration注解;

》2、檢視@EnableAutoConfiguration

@SpringBootApplication解析

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 已經存在了;

@SpringBootApplication解析

2.3、

@Inherited注解

@SpringBootApplication解析

》會通過注解超類來找依賴中的類,完成自動初始化類加載

* 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如何掃包的

@SpringBootApplication解析

》貼上一段注釋,第二段意思是,如沒有指明要掃的包,會從目前包路徑向下掃包;

是以,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