This is the first day of my participation in the Gwen Challenge in November. Check out the details: the last Gwen Challenge in 2021

Why the concept of auto-assembly

Spring is translated into English as Spring.

Spring did bring Spring to Java developers for a while.

However, as the project grew in size, Spring required more and more configuration, to exaggerate, “two hours of configuration, five minutes of Coding.”

As the concept of microservices grew in popularity, and the simplicity of various languages compared to Spring’s bloat, Spring Boot was born.

Spring Boot simplifies file configuration and can be launched directly. Spring Boot implements these functions based on two design strategies: out of the box and convention over configuration.

To create a new Spring Boot project, just name the project path, select the components we need, and get a running Spring Boot project.

Convention over configuration means that Spring Boot already has a lot of default parameters that you need by default (like Spring MVC, not Struts).

Now that Spring Boot already has some default parameters, you need to load them in, and the concept of autowiring is there.

How to implement automatic assembly of Spring Boot

We can understand what autowiring is by analyzing the code step by step.

Viewing the Startup File

Start Spring Boot by executing the Run method in the StorageApplication class. Class has the @SpringBootApplication annotation. This annotation specifies which class is the main configuration class of SpringBoot, and SpringBoot will run the main method of that class to start the SpringBoot project. So what exactly is the @SpringBootApplication annotation? Let’s take a look.

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

See the @SpringBootApplication annotation

Click in and we’ll find the following note:

@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 {
Copy the code

@SpringBootApplication is a composite annotation. Let’s look at it one by one.

See the @springBootConfiguration annotation

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

Click on the @springBootConfiguration annotation and see that the annotation is actually a Configuration class with the Configuration annotation.

And click on @Configuration to check it out,

@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface Configuration {  @AliasFor( annotation = Component.class ) String value() default ""; boolean proxyBeanMethods() default true; }Copy the code

The annotation is found with the @Component annotation, indicating that Spring’s configuration class is also a Component of Spring.

See the @enableAutoConfiguration annotation

The @enableAutoConfiguration annotation turns on the autowiring function.

Click inside to check it out:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {

	String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
Copy the code

Look at the @AutoConfigurationPackage annotation

So let’s take a look at what this annotation at sign AutoConfigurationPackage does.

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(AutoConfigurationPackages.Registrar.class)
public @interface AutoConfigurationPackage {

}
Copy the code

The annotation is the auto-configuration package that uses @import to Import a component into the Spring container, the Registrar.class component.

Look at the Registrar. The class

The SpringBoot project starts with the @SpringBootApplication annotation, which consists of three annotations:

@Configuration

@ComponentScan

@EnableAutoConfiguration

One @ EnableAutoConfiguration is to realize the automatic configuration of entrance, the annotations by @ Import annotations Import AutoConfigurationImportSelector, Load the meta-INF/Spring.Factories configuration information in this class. Then screen out the data with EnableAutoConfiguration as key, load into IOC container, realize automatic configuration function!

Reference documentation

  • SpringBoot: Take a serious look at the principle of automatic assembly
  • Analysis of SpringBoot automatic assembly principle
  • Use Debug in Intellij IDEA