Spring boot in this article is 2.3 Version 4
1. Spring boot features, understand the principle of automatic configuration
1.1. Dependency management
-
Dependency management for parent project:
<!-- Dependency management --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.4.RELEASE</version> </parent> <!-- ↓↓↓ spring-boot-starter-parent Parent project of ↓↓↓ --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.3.4.RELEASE</version> </parent> <!-- spring-boot-dependencies Dependency, which declares almost all dependencies and version numbers commonly used in development,Automatic version arbitration mechanism -->
-
Develop and import starter scenario launcher:
- See a lot of spring boot starter - *: * represents a certain scenario
- As long as the starter is introduced, we will automatically introduce all the conventional dependencies of this scenario
- All supported scenarios of SpringBoot, portal: https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-starter
- * - Spring boot starter: * is the dependency provided by the third party for us to integrate with Spring.
- The lowest level dependency of all scenario initiators
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.3.4.RELEASE</version> <scope>compile</scope> </dependency>
- No need to pay attention to the version number, automatic version arbitration
1,By default, no version can be written when importing dependencies, for example: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> 2,Introducing non version arbitrations jar,To write the version number.
- You can modify the default version number
1,see spring-boot-dependencies It specifies the current dependent version key. 2,Rewrite the configuration in the current project <properties> <mysql.version>5.1.43</mysql.version> </properties>
1.2. Automatically configure Web dependencies
-
Automatically configure Tomcat
- The spring boot starter web coordinate is introduced. The pom coordinate contains the dependency of spring boot starter tomcat, so Tomcat can be selected as the web server when starting
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
-
Default package structure:
- The components in the package where the main program is located and all its sub packages will be scanned by default
- No previous package scan configuration is required
- To change the scanning path, @ SpringBootApplication(scanBasePackages="com.atguigu")
-
Or use the @ ComponentScan annotation in the main startup class to specify the scan path.
@SpringBootApplication annotation Equivalent to @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan("com.atguigu.boot")
-
Various configurations have default values
-
- The default configuration is ultimately mapped to a class, such as MultipartProperties
- The value of the configuration file will eventually be bound to each class, which will create objects in the container
-
Load all auto configuration items on demand
-
- Very many starter s
- When the dependency of a scenario is introduced, the automatic configuration of the scenario will be enabled, which does not mean that all configuration classes will take effect.
- All spring boot autoconfigure functions are in the spring boot autoconfigure package
Simulate third-party dependencies integrated with SpringBoot
Overall structure
- Create new modules named XXX starter and XXX autoconfigure
- XXX starter project is responsible for POM Various dependencies are introduced into XML. If others want to use a function that the third party depends on, they can directly introduce the coordinates of XXX starter. The XXX starter project introduces the XXX autoconfigure project dependency.
- The XXX autoconfigure project is responsible for initializing the automatic configuration of this third-party dependency (such as registering the configuration class, xxxProperties... Into the container)
starter project
- In POM Introducing autoconfigure coordinates into XML
<dependencies> <dependency> <groupId>com.calm</groupId> <artifactId>calm-hello-spring-boot-autoconfigure</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
autoconfigure project
- In POM Add required dependencies to XML
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.4.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies>
- Properties
// Find the attributes prefixed with "prefix attribute value" in the configuration file and bind their values to this class @ConfigurationProperties(prefix = "calm.hello") public class HelloProperties { private String prefix; private String suffix; public String getPrefix() { return prefix; } public void setPrefix(String prefix) { this.prefix = prefix; } public String getSuffix() { return suffix; } public void setSuffix(String suffix) { this.suffix = suffix; } }
- service
public class HelloService { // Injection properties @Autowired private HelloProperties helloProperties; public String sayHello(String name){ return helloProperties.getPrefix() + "--" + name + "--" + helloProperties.getSuffix(); } }
- config configuration class
@Configuration // Enable the configuration binding function of HelloProperties class, and put it into the IOC container after binding. @EnableConfigurationProperties(HelloProperties.class) public class HelloServiceAutoConfiguration { // Judge that if there is no Bean of HelloService type in the IOC container, HelloService will be registered in the container. // The purpose is to use the HelloService customized by the user if the user has customized it. If there is no customization, use ours. @ConditionalOnMissingBean(HelloService.class) @Bean public HelloService helloService(HelloProperties helloProperties){ // Get HelloProperties from the container and set the value. helloProperties.setPrefix("register Bean Into the container."); return new HelloService(); } }
-
Create a new meta-inf / spring Factories file
- Where the value of EnableAutoConfiguration represents the autoconfiguration class to be loaded when the project starts
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.calm.config.HelloServiceAutoConfiguration
test
Create a new project and introduce a user-defined starter dependency. autoconfigure will automatically introduce it and pass the dependency.
<dependency> <groupId>com.calm</groupId> <artifactId>calm-hello-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
- Create another applicaiton Yaml profile
calm: hello: prefix: Configure prefix suffix: Configuration suffix
- Write a test class under the test package
@SpringBootTest @RunWith(SpringRunner.class) public class SpringBootStarterAutoConfiguration { @Autowired private HelloService helloService; @Test public void sayHello(){ System.out.println(helloService.sayHello("Zhang San")); } }