Three-body structure Spring Boot in @SpringBootApplication notes behind Quest

Outline

SpringBoot convention is larger than the configuration of the skill Let's inspiring, before I wrote the article "From SpringBoot to SpringMVC"
have been compared SpringBoot and SpringMVC these two frameworks, but in the end SpringBoot ultra-high signal to noise ratio and difficulty of use of the code let us image deep.

But in the end, regardless of the application or the nature SpringBoot SpringMVC remains a Spring-based applications, but in the latter face covered with a veil of mystery of it!

Back SpringBoot topic, we in the development of applications based SpringBoot, uses some new notes and class, officially because of its presence, only to JavaEE development duck to water. One of the comments that we use the most, was undoubtedly on SpringBoot application startup class @SpringBootApplication annotated

This article will look at it in the end is Gesha!



Behind @SpringBootApplication in the end is what?

@SpringBootApplication comment is actually a composite annotation SpringBoot provided, we take a look at its source code:

@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 { ... } 

I see clearly, which is a composite body, but the most important are three notes:

  • @SpringBootConfiguration

  • @EnableAutoConfiguration

  • @ComponentScan

We might call it "three-body structure" of it!

If we do not bother with this place of @SpringBootApplication three notes on startup class SpringBoot application notes found is not the problem:

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
public class TestSpringBootApplication { ... } 

Here are three analyze what effect these annotations!



@SpringBootConfiguration

Look at the code it, the code is written like this:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME) @Documented @Configuration public @interface SpringBootConfiguration { } 

This is explained from @SpringBootConfiguration @Configuration, both functions are marked as the current class to the class configuration, the current class and the method of example @Bean srping comment tag injected into the container, the instance name is the name of the method.

As @Configuration, I think in the era of non-SpringBoot we should not be unfamiliar to you, it is to configure the Spring container, namely the Spring IoC container JavaConfig form of configuration used by the class.

To the present situation, it seems to have nothing new! ! !



@EnableAutoConfiguration

And then continue to look at the code, the code is as follows:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import(AutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration { ... } 

@EnableAutoConfiguration comment enable automatic configuration, it can help SpringBoot apply to all eligible @Configuration configuration is loaded into the current IoC container can be briefly graphically indicate the following:

 
Behind component calling relationships @EnableAutoConfiguration

Next we control the source code to explain this process:

  • @EnableAutoConfiguration With AutoConfigurationImportSelector help, and the latter by implementing selectImports () to derive Configuration Methods
 
selectImports()
  • AutoConfigurationImportSelector class selectImports () method by calling Spring Core package inside SpringFactoriesLoader class loadFactoryNames () method
 
SpringFactoriesLoader.loadFactoryNames()
  • 最终通过 SpringFactoriesLoader.loadFactoryNames() 读取了 ClassPath 下面的 META-INF/spring.factories 文件来获取所有导出类。

而spring.factories 文件里关于 EnableAutoConfiguration 的配置其实就是一个键值对结构,样子大概长下面这样:

 
spring.factories

说了这么多,如果从稍微宏观一点的角度 概括总结 上述这一过程那就是:

从 ClassPath下扫描所有的 META-INF/spring.factories 配置文件,并将spring.factories 文件中的 EnableAutoConfiguration 对应的配置项通过反射机制实例化为对应标注了 @Configuration 的形式的IoC容器配置类,然后注入IoC容器。



@ComponentScan

@ComponentScan 对应于XML配置形式中的 <context:component-scan>,用于将一些标注了特定注解的bean定义批量采集注册到Spring的IoC容器之中,这些特定的注解大致包括:

  • @Controller
  • @Entity
  • @Component
  • @Service
  • @Repository

等等

对于该注解,还可以通过 basePackages 属性来更细粒度的控制该注解的自动扫描范围,比如:

@ComponentScan(basePackages = {"cn.codesheep.controller","cn.codesheep.entity"}) 

可见 这个注解也并不是什么新东西!




Guess you like

Origin www.cnblogs.com/zyy1688/p/11988885.html