Ask questions

In the Boot class of SpringBoot, there is only one @SpringBootApplication annotation, which is a compound annotation made by @SpringBootConfiguration,@EnableAutoConfiguration, and @compon EntScan consists of three annotations called SpringBoot

Before reading this article must understand the ConfigurationClassPostProcessor source explore three annotations and SpringBoot bedding”

Let’s address the two questions raised above

  • Sometimes an AutoConfiguration scanned by @ComponentSACn does not satisfy @Conditional conditions
  • How do the SpringBoot three annotations (@SpringBootConfiguration, @EnableAutoConfiguration, @ComponentScan) work
@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 {
    / / to omit...
}
Copy the code

1 @SpringBootConfiguration

It is equivalent to an @Configuration annotation, which is used to facilitate discovery in Spring tests. In essence, it belongs to @Configuration, which is a very important Configuration annotation of Spring. For details about how to use it, please refer to the official documentation. The source code is available at……… article

2 @EnableAutoConfiguration

2.1 AutoConfigurationImportSelector

It implements the function of automatic assembly, is a DeferredImportSelector, concrete analysis can refer to the ConfigurationClassPostProcessor source explore three annotations and SpringBoot bedding “in section 1.4

2.2 AutoConfigurationPackage

Are a ImportBeanDefinitionRegistrar AutoConfigurationPackages registered to the BeanFactory, used for other functions, the official written comments are JPA entity Scanner, but I don’t see a more common one right now.

3 @ComponentScan

By default, it scans @Component and adds two excludeFilters to SpringBoot:

  • TypeExcludeFilter, which is used in Spring Test
  • AutoConfigurationExcludeFilter, ignore @ Configuration and EnableAutoConfiguration are so often SpringBoot not mistake the scanning to automatic assembly

4 Problem Solving

  • Sometimes an AutoConfiguration scanned by @ComponentSACn does not satisfy @Conditional conditions

Normally AutoConfigurationExcludeFilter can skip, but if your AutoConfiguration class do not conform to the general specification, will be @ ComponentScan sweep to early, affect the loading sequence

  • How do the SpringBoot three annotations (@SpringBootConfiguration, @EnableAutoConfiguration, @ComponentScan) work

Is essentially composed of @ Configuration + @ ComponentScan + AutoConfigurationImportSelector, are implemented in ConfigurationClassPostProcessor