1, springboot Basics
1.1 general
Spring Boot can easily and quickly create applications based on the spring framework, which enables coding, configuration, deployment and monitoring. Spring Boot has attracted much attention in the industry because it simplifies the complexity and makes the development extremely simple and fast.
1.2 characteristics
- Ability to quickly create Spring based applications
- You can directly use the java main method to start the embedded Tomcat server and run the Spring Boot program without deploying the war package file
- Provide the agreed starter POM to simplify Maven configuration and make Maven configuration simple
- Automatic configuration: according to Maven dependency configuration of the project, Spring boot automatically configures ssm, etc
- It provides functions such as health check of the program
- Basically, you can use annotation configuration without using XML configuration files at all
1.3 four cores
-
Automatic configuration (key)
-
Start dependence (key)
-
Actor (non key)
-
Command line interface (non key)
2, Building the springboot framework
2.1 create a Module and select Spring Initializr for quick build
2.2 setting GAV coordinates and pom configuration information
2.3 select Spring Boot version and dependency
2.4 set the module name, Content Root path and directory of module files
They are: set the module name, set the module root directory, and set the template file directory
2.5 creation completed
3, Explanation of configuration file
3.1 form of test and coding
-
The Application in the springboot package is the entry point of the program
-
All classes must be used in the same directory or subdirectory of Application
3.2 pom.xml file interpretation
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!--inherit SpringBoot A parent project of the framework, all self-developed Spring Boot Must be inherited--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <!--Of the current project GAV coordinate--> <groupId>com.aiit.springboot</groupId> <artifactId>001-springboot-first</artifactId> <version>1.0.0</version> <name>001-springboot-first</name> <!--maven Project name, which can be deleted--> <name>001-springboot-first</name> <!--maven Item description, which can be deleted--> <description>Demo project for Spring Boot</description> <!--maven Property configuration, which can be used in other places ${}Reference by--> <!--Compiling environment--> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!--Springboot frame web Start dependent project--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--springboot Framework test start dependency--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <!--springboot Project packaged and compiled plug-ins--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3.3 catalog interpretation
- .mvn|mvnw|mvnw.cmd: script operation is used to execute maven related commands. It is rarely used in China and can be deleted
- . gitignore: when using the version control tool git, set some content to ignore the submission
- static|templates: the directory where files are stored in the template technology
- application.properties: the configuration file of SpringBoot. Many integrated configurations can be found in this file
Configure, such as Spring, Spring MVC, Mybatis, Redis, etc. Is currently empty - Application.java: the entry for the execution of the SpringBoot program. Execute the main method in the program, and SpringBoot will start
3.3 core configuration file properties
- This file is used by default
- By modifying application The properties configuration file is used to modify the default tomcat port number and the configuration method of the properties property file of the root key value pair of the upper and lower files of the project
3.4 core configuration file yml file
- yaml is an intuitive data serialization format that can be recognized by computer, which is easy to be read by human beings
- Yaml is similar to xml, but its syntax is much simpler than xml. There must be a space between the value and the colon configuration item in front. The yml suffix can also be used after yaml
3.5 do the properties and yml files exist at the same time?
-
First of all, the functions of the two files are the same
-
When the two format configuration files exist at the same time, the properties configuration file
3.6 multi environment configuration (properties are the same as yml file)
- In the actual development process, the project will go through many stages (Development - > test - > online)
- The configuration of each stage is also different, such as port, context root, database, etc
- In order to facilitate switching between different environments, SpringBoot provides multi environment configuration
Step 1: create a configuration file for each environment. The name must be identified by application environment properties|yml
Step 2: in the general configuration file application Properties to activate the environment
4, Custom configuration item
In the core configuration file of SpringBoot, in addition to using built-in configuration items, we can also customize the configuration, and then use the following annotation to read the attribute value of the configuration
4.1 use @ value annotation
- This annotation is mainly used when there are few user-defined configuration items, and can be assigned one by one
- properties file
- yml file
Use @ Value() annotation
@Controller public class IndexController { @Value("${school.name}") private String school_name; @Value("${age}") private Integer age; @RequestMapping(value = "/hello") @ResponseBody public String hello(){ return "hello world"+school_name; } }
Debugging results
4.2 use @ ConfigurationProperties
Map the whole file into an object, which is used when there are many custom configuration items
4.2.1 cases
-
properties file
-
Create an object to associate this configuration file
-
Test and access
-
The annotation must be used with a prefix. Otherwise, it cannot be used. And the prefix is uniform
4.3 warning resolution
- The following warning will appear at the top of the newly created class using the above annotation:
- In POM XML add the following configuration to eliminate
<!--Solve use@ConfigurationProperties Warning problem with annotation--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
- Warning elimination
4.4 Chinese garbled code solution
If there is Chinese information in the SpringBoot core configuration (application.properties/yml) file, garbled code will appear
- Generally, Chinese is not recommended in the configuration file (except notes)
- If yes, it can be converted to ASCII code first