[Project combat] Getting started with SpringBoot source code from 0-SpringBoot automatic assembly and @SpringBootApplication annotation source code

1. What is SpringBoot automatic assembly?

SpringBoot provides the function of automatic assembly, which makes it easier for developers to inject dependencies. It means that when the SpringBoot application is started, it will automatically download the required jar package from the local warehouse or the remote warehouse according to the dependencies of the project, and automatically complete the dependency injection, generate Bean objects, and then hand them over to the Spring container for management.

Using SpringBoot automatic assembly can make it easier for developers to manage dependencies between classes and improve development efficiency. However, it should be noted that when using automatic assembly, it is necessary to ensure that the dependencies of the classes are correct, otherwise there may be some problems that are difficult to troubleshoot.

2. The principle of SpringBoot automatic assembly

The @SpringBootApplication annotation is added to the startup class.
The principle of automatic assembly is based on Spring's dependency injection and aspect-oriented programming features. In SpringBoot, when a class is annotated with annotations such as @Component or @Service, Spring will automatically scan the dependencies of the class and try to inject these dependencies into the class through automatic assembly.

3. Introduction to @SpringBootApplication annotation

The @SpringBootApplication annotation is a composite annotation

The @SpringBootApplication annotation consists of three sub-annotations, whose functions are as follows

  • @SpringBootConfiguration tells SpringBoot that this is a main configuration class
  • @ComponentScan enables component scanning
  • @EnableAutoConfiguration Turn on automatic configuration

4. There are two main ways of SpringBoot automatic assembly

There are two ways of automatic assembly: constructor automatic assembly and setter method automatic assembly.

4.1 Constructor autowiring

Autowiring is achieved by defining a constructor in the class and injecting the dependencies of the class into the constructor.

When using constructor auto-assembly, you need to add @Autowired annotations to annotations such as @Component or @Service to tell Spring that the dependency needs to be auto-assembled.

4.2 Setter method automatic assembly

Autowiring is achieved by defining a setter method in the class and injecting the dependencies of the class in the method.

When using the setter method for automatic assembly, you need to add the @Autowired annotation to the annotations such as @Component or @Service to tell Spring that the dependency needs to be automatically assembled.

4.3 Other automatic assembly methods

In addition to constructor autowiring and setter method autowiring, Spring also supports other autowiring methods, such as annotating property values ​​in autowiring configuration files through @ConfigurationProperties annotations.

Five, @SpringBootApplication annotation source code

First look at the code of the startup class

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

The source code of this annotation is as follows:

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

Annotation superposition is completely superimposed by the following three annotations.

5.1 @SpringBootConfiguration tells SpringBoot that this is a main configuration class

This annotation is an annotation of Spring Boot, and the source code is as follows:

@Target({
    
    ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
    
    
		@AliasFor(annotation = Configuration.class)
		boolean proxyBeanMethods() default true;
}

As can be seen from the source code,
@SpringBootConfiguration is completely @Configuration annotation,
@Configuration is an annotation in Spring, indicating that this class is a configuration class, so
you can do some things that configuration classes can do in the startup class, such as injecting a Bean.

So, we often inject some beans into @SpringBootConfiguration

5.2 @ComponentScan starts component scanning

The annotations in Spring, the annotations of package scanning, are used when the project starts to scan the same level of the startup class and the beans in the lower-level packages.

5.3 @EnableAutoConfiguration Turn on automatic configuration

  • Turn on automatic configuration, and quickly inject beans into the IOC container when the project starts.
  • @EnableAutoConfiguration annotation function: a form of @Import
    The source code is as follows:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
    
    

I saw a @Importcomment, using the ImportSelector method

Guess you like

Origin blog.csdn.net/wstever/article/details/129978219