【SpringBoot-注解】@SpringBootApplication

1. Understanding @SpringBootApplication

@SpringBootApplication is used to activate the features of @EnableAutoConfiguration, @ComponentScan, and @Configuration annotations.
in:

  • @EnableAutoConfiguration is responsible for activating the spring boot automatic assembly mechanism
  • @ComponentScan is responsible for activating the scanning of @Component
  • @Configuration is responsible for declaring classes marked as configuration

It can be stated that @SpringBootApplication is equivalent to @EnableAutoConfiguration, @ComponentScan, @Configuration

But after 2.0 there is a change

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

Instead of using the default, @ComponentScan adds the excluded TypeFilter implementation:

public class AutoConfigurationExcludeFilter implements TypeFilter, BeanClassLoaderAware
public class TypeExcludeFilter implements TypeFilter, BeanFactoryAware {
    
    
  • TypeExcludeFilter: It is used to find the registered TypeExcludeFilterBean in BeanFactory as the loan execution object.

  • AutoConfigurationExcludeFilter: Used to exclude other classes that both mark @Configuration and @EnableAutoConfiguration.

The @SpringBootApplication annotation has changed since SpringBoot1.4. @SpringBootApplication did not mark @Configuration, but changed to @SpringBootConfiguration, there is no difference in operation.
The relationship becomes:

  • @SpringBootConfiguration
    • @Configuration
      • @Component

Although @ComponentScan focuses on @Component, we also see that @SpringBootConfiguration is a multi-level reference to @Component, so it can also be scanned.

The @Service, @Repository, and @Controller we use are all derived annotations belonging to @Component, also called Spring mode annotations.

2. Related attributes

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

    @AliasFor(
        annotation = ComponentScan.class,
        attribute = "nameGenerator"
    )
    Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;

    @AliasFor(
        annotation = Configuration.class
    )
    boolean proxyBeanMethods() default true;

You can see that the attributes are marked with the @AliasFor annotation, which is the attribute used to bridge other annotations. The @AliasFor annotation can alias the properties of one or more annotations to an annotation.

So in summary, @SpringBootApplication is an aggregate annotation, which we mark on the boot class.

おすすめ

転載: blog.csdn.net/daohangtaiqian/article/details/131697250