Framework learning - SpringBoot core annotation

1, Entry class and @ SpringBootApplication

Spring Boot projects generally have an "Application" entry class, in which there will be a main method, which is the entry method of a standard Java Application@ SpringBootApplication annotation is the core annotation of Spring Boot. It is actually a composite annotation. We can see the following by opening the source code of @ SpringBootApplication annotation:
 

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class))
public @interface SpringBootApplication {
 
    Class<?>[] exclude() default {};
 
    String[] excludeName() default {};
 
    @AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
    String[] scanBasePackages() default {};
 
 
    @AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
    Class<?>[] scanBasePackageClasses() default {};
 
}

@SpringBootApplication itself is an annotation, and its annotation description has many annotation definitions, which is called "composite Annotation". It references the @ SpringBootApplication annotation, and its own annotations are all effective.
The notes above are explained as follows:

(1)@Target(ElementType.TYPE)
@Target refers to the action target of the annotation, and ElementType refers to the type of action target:

@Target(ElementType.TYPE)   //Interface, class, enumeration, annotation
@Target(ElementType.FIELD) //Field, enumerated constant
@Target(ElementType.METHOD) //method
@Target(ElementType.PARAMETER) //Method parameters
@Target(ElementType.CONSTRUCTOR)  //Constructor
@Target(ElementType.LOCAL_VARIABLE)//local variable
@Target(ElementType.ANNOTATION_TYPE)//annotation
@Target(ElementType.PACKAGE) ///Package   

(2)@Retention(RetentionPolicy.RUNTIME)

@Retention Represents the reserved position of the annotation, where RetentionPolicy Representative retention rules:
@Retention(RetentionPolicy.SOURCE)   //Annotations exist only in the source code and are not included in the class bytecode file
@Retention(RetentionPolicy.CLASS)     // The default retention policy is that the annotation will exist in the class bytecode file, but cannot be obtained at run time,
@Retention(RetentionPolicy.RUNTIME)  // Annotations exist in the class bytecode file and can be obtained by reflection at run time

(3)@Documented
Note that the annotation will be included in the javadoc

(4)@Inherited
Note that the subclass can inherit the annotation in the parent class

(5)@SpringBootConfiguration
For the "@ SpringBootConfiguration" annotation, open its source code:

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

We can find that it is also a "composite Annotation". It is not difficult to find that it is actually a packaging of @ Configuration. We can think of it as @ Configuration annotation. It is recommended to use @ SpringBootConfiguration instead of @ Configuration annotation in the project.

(6)@EnableAutoConfiguration
This annotation indicates "turn on automatic configuration". We do not configure MVC component loading information such as Tomcat container (including port) and dispatcherServlet in Spring Boot, web XML files, etc. after specifying the annotation, Spring Boot helps us initialize them.
When we enable the @ EnableAutoConfiguration annotation and start automatic configuration, the annotation will enable spring boot to automatically configure the configuration items of the project according to the dependent jar packages in the project. For example, when we add spring boot starter web dependency, spring MVC dependency will be introduced into the project, and spring boot will automatically configure tomcat and spring MVC:

 

In fact, there are many other core dependencies that the Web needs

(7)@ComponentScan
This annotation will automatically scan all @ Controller, @ Service, @ Repository, @ Component classes under the package path. If the package path is not configured, the peer directory and subdirectory of the class where @ SpringBootApplication is located will be scanned by default in Spring Boot.

2, Turn off auto configuration
From the above description, we can know that Spring Boot will automatically make configuration according to the jar package dependencies configured in the project, and Spring Boot supports many configurations. We can observe the Spring Boot autoconfigure dependencies introduced in the sample project. Many dependencies are introduced below:

Many times, we may not need so many dependency introductions. At this time, we can turn off automatic configuration to solve this problem.
For example, we don't need to configure Redis automatically. You can set it as follows:

package cn.springboot.test;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
 
@SpringBootApplication(exclude={RedisAutoConfiguration.class})
//The core annotation of the Sprnig Boot project is mainly used to enable automatic configuration
public class SpringbootTestSimpleApplication {
    //The main method serves as the entry point for project startup
    public static void main(String[] args) {
        SpringApplication.run(SpringbootTestSimpleApplication.class, args);
    }
}

That is, use the "exclude" parameter to exclude unnecessary auto configuration dependencies.

3, Custom Banner

This character drawing is the Banner of Spring Boot. We can customize this character drawing.
First, let's define a character picture and recommend a website: http://www.bootschool.net/ascii
After opening, we can enter a string of words casually, such as "Hello World":

 

We store the character drawing in a txt file, then save it as "banner.txt" and put it into the resources static resource folder of the test project:

Then restart the project and you will see that banner has become our custom:

 

Of course, we can also close the banner, define a SpringApplication in the startup class, and set its banner property to "Mode.off":

package cn.springboot.test;
 
import org.springframework.boot.Banner.Mode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
//The core annotation of the Sprnig Boot project is mainly used to enable automatic configuration
public class SpringbootTestSimpleApplication {
    //The main method serves as the entry point for project startup
    public static void main(String[] args) {
        //SpringApplication.run(SpringbootTestSimpleApplication.class, args);
        SpringApplication springApplication = new SpringApplication(SpringbootTestSimpleApplication.class);
        springApplication.setBannerMode(Mode.OFF);
        springApplication.run(args);
    }
}

 

 

  

Added by wefollow on Mon, 03 Jan 2022 19:19:13 +0200