What is a SpringBoot

The website explains that, in a nutshell, SpringBoot can be used to quickly create production-grade Spring applications.

What is SpringBoot autowiring

If there is no SpringBoot auto-assembly, if we need to start a third-party service, we need to write tedious configuration files, configuration classes. Autowiring, on the other hand, simply imports the dependency and then starts the main method of the startup class. And we can customize the properties through the application configuration file

How does SpringBoot implement auto-assembly



You can see that on the startup class, there’s only one annotation@SpringBootApplicationClick to enter

The annotation is found to be a composite annotation, except for the above four meta-annotations, the focus is on the following two annotations

@SpringBootConfiguration

@EnableAutoConfiguration

See first@SpringBootConfiguration

And you can see that this is actually a@ConfigurationModify the class

Explain that @Configuration is simply injecting beans under the modified class. Here’s an example:

As you can see, I added an @bean to the startup class so that it can be injected into the IOC container at startup time

Summary: The first annotation @SpringBootConfiguration is to declare that the boot class is also a configuration class that can import the beans we need

Finally, @enableAutoConfiguration

It’s also a composite annotation, so at sign AutoConfigurationPackage

AutoConfigurationPackages is introduced. The Registrar. The class, Is to manage the package of the class that the annotation modifies as the package that is automatically configured when the SpringBoot application starts. That’s why we put the Boot class of SpringBoot under the outermost package.

Click on the Registar introduced by @import and notice the second parameter, new PackageImports

You can see that you get the package name of the package that started the class

Then look at another annotation @ Import (AutoConfigurationImportSelector. Class)



Introduced AutoConfigurationImportSelector. Class is the key

Look at his process method

Continue to see getAutoConfigurationEntry method

See getCandidateConfigurations method

From this sentence, we can judge thatReal AutoConfigurationImportSelector. The class is reading the meta-inf/spring. The factories below package path, loaded into the container

Find this Spring.Factories and you’ll find 130 classes configured

Pick any one and look it up in Spring.Factories

The discovery is right here

Conclusion:

1. Automatic assembly of SpringBoot is performed through @SpringBootConfiguration and @enableAutoConfiguration Add to the IOC container, you can also use custom import beans 3.@EnableAutoConfiguration to automatically inject the IOC container by scanning 130 classes under Spring.Factories