Automatic assembly-combined with source code analysis
1 Environmental preparation
1.1 Build a simple project
Start with a web project to analyze automatic assembly, so we need to quickly build a Hello World
1.2 Official documentation
Official website link:
https://docs.spring.io/spring-boot/docs/2.4.4-SNAPSHOT/reference/html/using-spring-boot.html#using-boot-configuration-classes
According to the official documentation:
as long as you mark @SpringBootApplication on the startup class, you can have the following 3 annotation functions
- @EnableAutoConfiguration: Automatic import configuration function
- @ComponentScan: Scan the components of the annotated package
- @Configuration: Annotate that this is a configuration class
1.3 View source code
Automatic configuration key points
1. Automatic assembly of components
xxxAutoConfiguration
2. Automatic configuration
xxxProperties, through which you can customize configuration items
3.dubug ture
See which components are in effect
1. Location of static resources
Remarks: Based on the version number V2.4.3, the location of
different versions may be somewhat different
1. Default location (loaded at startup)
(1). Location
- classpath:/META-INF/resources/webjars/
- classpath:/META-INF/resources/
- classpath:/resources/
- classpath:/static/
- classpath:/public/
(2) Priority
resources> static (default)> public
generally rarely use webjars/
(3). Corresponding source code
-
WebMvcAutoConfiguration
-
WebProperties
2. Custom (will override the default configuration)
(1) Customize in the configuration file
application.properties
spring.mvc.static-path-pattern=/自定义路径
(2) Corresponding source code
- WebMvcProperties