The secret behind the @SpringBootApplication annotation of the SpringBoot startup class

When using the SpringBoot project, you will find that no matter what you do, the startup class is inseparable. It is the only entry point to the program, so what has he done for us? This article mainly analyzes @SpringBootApplication.

1. Startup

@SpringBootApplication
public class Application {
    
    
	
	public static void main(String[] args) {
    
     
		SpringApplication.run(Application.class,args); 
	}
	
}

二、@SpringBootApplication

@SpringBootApplication: Spring Boot application annotation on a certain class indicates that this class is the main configuration class of SpringBoot.

SpringBoot should run the main method of this class to start the SpringBoot application;

Relevant configuration startup is done by this annotation to help us complete, click in to find out

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

}

Clicking in will find that there are still many annotations in his annotation class, which is a custom combination annotation.

Next, let's explain one by one the annotations of his combination.

1、@Target(ElementType.TYPE)

@Target explains the range of objects modified by Annotation. The
values ​​(ElementType) are:

1. CONSTRUCTOR: used to describe the constructor
2. FIELD: used to describe the domain
3. LOCAL_VARIABLE: used to describe local variables
4. METHOD: used to describe the method
5. PACKAGE: used to describe the package
6. PARAMETER: used to describe the parameters
7.TYPE: used to describe class, interface (including annotation type) or enum statement

2、@Retention(RetentionPolicy.RUNTIME)

Annotations can be divided into 3 categories according to the life cycle:

1. RetentionPolicy.SOURCE: The annotation is only kept in the source file. When the Java file is compiled into a class file, the annotation is abandoned;
2. RetentionPolicy.CLASS: The annotation is kept in the class file, but the jvm is abandoned when the class file is loaded. It is the default life cycle;
3. RetentionPolicy.RUNTIME: The annotation is not only saved in the class file, but still exists after the JVM loads the class file;

3、@Documented

This annotation is only used to mark whether it will be recorded when javadoc is generated.

You can use @Documented to annotate when customizing annotations. If you use @Documented to annotate, @Documented annotations will be displayed when javadoc is generated.

4、@Inherited

@Inherited is a logo used to modify annotations, which will be used in custom annotations

First customize an annotation

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface ATable {
    
    
    public String name() default "";
}

The following is a scenario where custom annotations are used.

The role of @Inherited in the
class inheritance relationship In the class inheritance relationship, the subclass will inherit the annotations modified by @Inherited in the annotations used by the parent class

@ATable 
public class InheritedBase {
    
    
}
public class MyInheritedClass extends InheritedBase  {
    
    
}

The role of @Inherited in the
interface inheritance relationship In the interface inheritance relationship, the child interface will not inherit any annotations in the parent interface, regardless of whether the annotations used in the parent interface are modified by @Inherited

@ATable
public interface IInheritedInterface {
    
    
}
public interface IInheritedInterfaceChild extends IInheritedInterface {
    
    
}

The role of @Inherited in the class implementation interface relationship When the
class implements the interface, it will not inherit any annotations defined in the interface.

@ATable
public interface IInheritedInterface {
    
    
}
public class MyInheritedClassUseInterface implements IInheritedInterface {
    
    
}

5、@SpringBootConfiguration

Marked on a certain class, indicating that this is a Spring Boot configuration class

Click in and you will find that it is actually a custom annotation

@Configuration learning spring should be familiar to him.
Role: Specify that the current class is a configuration class. When using spring, it is all xml configuration at the beginning. It is this annotation that opens the class configuration method.

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

}

6、@EnableAutoConfiguration

What we need to configure before, Spring Boot will automatically configure it for us;

@EnableAutoConfiguration tells SpringBoot to turn on the automatic configuration function; so that the automatic configuration can take effect;

Click into it and you will find @Import. To put it bluntly, he uses the support of @Import to collect and register bean definitions related to specific scenarios.

@Import role: used to import other configuration classes

And @EnableAutoConfiguration also uses the help of @Import to load all the bean definitions that meet the auto-configuration conditions into the IoC container, nothing more!

@SuppressWarnings("deprecation")
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
    
    

}

EnableAutoConfigurationImportSelector: the selector of which components
are imported; a lot of automatic configuration classes (xxxAutoConfiguration) will be imported into the container;
Insert picture description here

The approximate process is:

When Spring Boot starts, through the EnableAutoConfigurationImportSelector class,
the value specified by EnableAutoConfiguration is obtained from META-INF/spring.factories under the classpath (the screenshot above),
and these values ​​are used as automatic configuration by the creation method of the full class name reflection Import the class into the container, and the auto-configuration class will take effect,
helping us with the auto-configuration work;

Before we need to configure things by ourselves, the automatic configuration classes have been configured for us. This is the reason why spring is used with springboot, and springmvc does not need to configure view resolvers, database connection pools, transactions and other configurations. It works right out of the box.

Of course, springboot also provided me with a way to modify the configuration, that is, through the yml or propertie file to modify the default value of the configuration that springboot has configured for us.

7、@ComponentScan

Function: Used to specify the package that Spring will scan when creating a container through annotations

We can fine-grainly customize the scope of @ComponentScan automatic scanning through attributes such as basePackages. If not specified, the default Spring framework implementation will scan from the package where the @ComponentScan class is declared.

@ComponentScan("com.gzl")

This is why the springboot startup class is placed outside the package.

3. Can you start the project without using this annotation?

Replace @SpringBootApplication with the following three annotations, and it can still start normally.

package com.gzl.cn;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
    
    
	
	public static void main(String[] args) {
    
     
		SpringApplication.run(Application.class,args); 
	}
	
}

Guess you like

Origin blog.csdn.net/weixin_43888891/article/details/110457235