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 Initializr
and selection web module can easily build a web project.
If you follow the past, how we do it
- Created maven project and import the relevant coordinates SpringMVC
- Create a profile SpringMVC
- Open comments scans in the configuration file, and specify the package to be scanned
- Configuration component may be required (view resolver, interceptor, the exception handler, etc.) in the configuration file
- 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 SpringBoot
after 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 @SpringBootApplication
annotated 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.
We found that it is a combination of notes, notes there are a few more important, we need to know:
-
@ComponentScan
name suggests, is scanned for annotations, the default scan main class where the package comment -
@SpringBootConfiguration
then point Inside, the@Configuration
notes, but it was indicating that this is a configuration class -
@EnableAutoConfiguration
enable automatic configuration, this is more important, we point into
or a combination of notes, there are two important notes-
@AutoConfigurationPackage
Automatic configuration package, and its@ComponentScan
difference is not the same as the object being scanned -
@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: thisAutoConfigurationImportSelector
category 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.