SpringBoot learning --- simple way to see that point thing the principle of automatic configuration

Prior to the introduction, SpringBoot this thing has a strong point that it can automatically configure some bean objects, which greatly simplifies your configuration file (whisper bb, may not need it anymore)


As the saying goes bald can only become strong


To the introduction above, the use Spring Initializrand selection web module can easily build a web project.

If you follow the past, how we do it

  1. Created maven project and import the relevant coordinates SpringMVC
  2. Create a profile SpringMVC
  3. Open comments scans in the configuration file, and specify the package to be scanned
  4. Configuration component may be required (view resolver, interceptor, the exception handler, etc.) in the configuration file
  5. In web.xml configuration DispactherServlet, you may needCharacterEncoding

These are often we do, sometimes we just want to write a the HelloWorld , but most of the time no use in writing business, it touches used in the configuration files. And SpringBootafter you've created the project, it will help us to automatically configure the components we need, so we created before the project, but no configuration file can be up and running!


Its automatic equipment is how it happened?


We will find SpringBoot project a master class, a top @SpringBootApplicationannotated with this annotation can tell the computer which is our main SpringBoot class, a little magic, but we are picking up points into its skin.
Here Insert Picture Description
We found that it is a combination of notes, notes there are a few more important, we need to know:

  1. @ComponentScan
    name suggests, is scanned for annotations, the default scan main class where the package comment

  2. @SpringBootConfiguration
    then point Inside, the @Configurationnotes, but it was indicating that this is a configuration class

  3. @EnableAutoConfiguration
    enable automatic configuration, this is more important, we point into
    Here Insert Picture Description
    or a combination of notes, there are two important notes

    1. @AutoConfigurationPackage
      Automatic configuration package, and its @ComponentScandifference is not the same as the object being scanned

    2. @Import
      A: Reporting, this is not the underlying Spring Annotations do?
      B: Yes, it is up to the IOC to import some of the components
      A: this AutoConfigurationImportSelectorcategory yet?
      B: yep! This class has a role, it will eventually turn eighteen after the landscape, will return 113 configuration class, then we will load the default configuration class (who are interested can study on their own, which is to scan jar jar under the relevant package, then into a package properties, and all of the return key)
      so our IOC from these containers have the components

In fact SpringBoot help us complete the automatic configuration of things, if we want to change some components inside, we may have to go to the configuration, and then after this record.

That those components imported there are many versions, SpringBoot is how to help us choose it?

Do be careful, next time to record it.

Published 20 original articles · won praise 15 · views 4171

Guess you like

Origin blog.csdn.net/Jokeronee/article/details/104929938
Recommended