Focus on Spring Boot+ micro services, mini programs, Flutter, Android, regularly share articles and video tutorials, reply to Java materials after attention, receive the learning dry goods carefully prepared for you!
Hello, everyone, I am “chasing dream snail”, you can reply “Java information” in the background of the public account to obtain the information of skill improvement, it is absolutely dry goods.
In addition: there are monthly book sending activities, attention to the public back to send books will have the opportunity to get good books.
Hello, everyone, I am “chasing dream snail”, you can reply “Java information” in the background of the public account to obtain the information of skill improvement, it is absolutely dry goods.
This article is the third in the Spring Boot series, and knowing the previous articles will help you understand it better:
2.Spring Boot (2) Basic Configuration of Spring Boot
preface
(I) automatic configuration of Spring Boot
SpringBoot automatic configuration, automatic configuration is a project as another project to run the dependency library call, automatic use of Bean objects inside, Dynamic changes can be made (personal opinion). Let’s explore how Spring Boot implements automatic configuration.
Let’s take a case to explain the principle of SpringBoot automatic configuration.
To make things easier to understand, here is a simple example:
In the project, when a class exists, how to automatically configure the Bean object of this class, and can configure the Bean properties in the application. Properties, dynamic change, OK, such a requirement, let’s look at how to implement.
1. Create a Starter Maven project
-
To create a Project, I use IDEA tool as an example to create a Maven project as follows: Click create project —- and select Maven– choose Maven-archetype-QuickStart, and then click Next
-
Fill in GroupId and Artifactid and click Next
-
Select the Maven installation directory and the Maven Resource Update directory, and click Next
-
Fill in the project name and project storage directory and click Finish
At this point, a project is created.
2. Add dependencies
When we use SpringBoot automatic configuration, we need to add automatic configuration dependencies, as follows:
<! --> <dependency> <groupId>org.springframework.boot</groupId> < artifactId > spring - the boot - autoconfigure < / artifactId > < version > 2.1.9. RELEASE < / version > < / dependency >Copy the code
3. Property configuration
package org.cxzc.myyoung; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix ="hello"Public class HelloServiceProperties {private static Final String MSG ="world"; private String msg = MSG; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; }}Copy the code
This is a basic entity class, the same as the type-safe attribute acquisition in the previous section, which can be set in application.properties. If not, the default is hello
4. Judgment basis class
package org.cxzc.myyoung; public class HelloService { private String msg; public StringautoInfo() {return "Hello" + msg; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; }}Copy the code
We need to create a Bean for this class based on whether it exists or not. This class can also be a third party library class.
5. Automatic configuration classes
package org.cxzc.myyoung; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration@EnableConfigurationProperties(HelloServiceProperties.class)@ConditionalOnClass(HelloService.class)@Condit ionalOnProperty(prefix ="hello",value = "enable",matchIfMissing = true)public class HelloServiceAutoConfiguration { @Autowired private HelloServiceProperties helloServiceProperties; @Bean public HelloService helloService() { HelloService helloService = new HelloService(); helloService.setMsg(helloServiceProperties.getMsg()); return helloService; }}Copy the code
1. Use the parameters provided by HelloServiceProperties in the automatic configuration class.
ConditionalOnClass to determine whether the HelloService class exists in the classpath and automatically configure the Bean if it does not exist in the container
3. The @conditionalonProperty annotation indicates the conditions to be fulfilled by the specified attribute
< SRC /main/resources > < META-INF/spring.factories > < SRC /main/resources >
Create a new directory and fill in the spring.factories file. If multiple west and East configurations need to be separated by commas (,), and if multiple configurations need to be newlines, add \.
1. No line breaks
org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.cxzc.myyoung.HelloServiceAutoConfigurationCopy the code
2, line
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\org.cxzc.myyoung.HelloServiceAutoConfigurationCopy the code
Note:
In the IntelliJ IDEA tool, we need to create a directory (directory) and then set it to Resources root, as follows:
Right-click —– Mark Directory as—– Resource Root. The diagram below:
All of our autoconfiguration has been added and configured up to this point. Now to use autoconfiguration, we need to create a New SpringBoot project. And import the auto-configuration items above as dependencies.
In most cases, you need to upload the auto-configuration class to the Maven server as a dependency repository, and then reference it directly in the project, or install it in the local library.
7. Create Module (SpringBoot) project
The process for creating a Module is as follows: Select the Project and right click —-. New Module. This Module is a SpringBoot Project. Press (F3,F4) to select the new Module after the creation is successful. Press the specific key to exclude the computer Settings. Mine is F3 and then open the Module Settings, and select the plus sign on the right to add dependencies, as shown below:
1. Enter the configuration item
2. Select Module
3. Add automatic projects as dependency libraries
Clicking OK makes auto configuration a dependency library
8. Add automatic configuration dependencies
After creating the SpringBoot project and referencing automatic configuration, we need to add the following dependencies to the pom.xml file of the new Module (SpringBoot project) :
<! <dependency> <groupId>org.cxzc.myyoung</groupId> <artifactId>springboot_3</artifactId> < version > 1.0 - the SNAPSHOT < / version > < / dependency >Copy the code
We inject HelloService directly into the entry class of the SpringBoot project. This class is not configured in SpringBoot. This is done by automatic configuration.
package org.cxzc.my.springbootmy; import org.cxzc.myyoung.HelloService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController@SpringBootApplicationpublic class SpringbootmyApplication { @Autowired private HelloService helloService; @RequestMapping(value ="/",produces = "text/plain; charset=UTF-8") public String index() {return helloService.autoInfo(); } public static void main(String[] args) { SpringApplication.run(SpringbootmyApplication.class, args); }}Copy the code
If necessary, we can modify the MSG content in the SpringBoot project application. Properties and change the information
server.tomcat.uri-encoding=UTF-8spring.http.encoding.charset=UTF-8spring.http.encoding.enabled=truespring.http.encoding. Force = truespring. Messages. Encoding = utf-8 hello. MSG = automation configuration, change here change automaticallyCopy the code
Running the SpringBoot project results in the following image:
Well, custom automatic configuration to here is complete, if partners have questions, you can add group, we progress together
Download address of this case:
https://github.com/ProceduralZC/itcxzc/tree/master/springboot_3
Focus on Spring Boot+ micro services, mini programs, Flutter, Android, regularly share articles and video tutorials, reply to Java materials after attention, receive the learning dry goods carefully prepared for you!
Important things to say three times:
There are monthly book sending activities, attention to the public account back to send books have the opportunity to get good books.
There are monthly book sending activities, attention to the public account back to send books have the opportunity to get good books.
There are monthly book sending activities, attention to the public account back to send books have the opportunity to get good books.