table of Contents
1 Getting started
1.1 Introduction
Simplify Spring application development, integrate Spring technology stack, and JavaEE one-stop solution. (Use the relationship between keras and TensorFlow to understand Spring Boot and Spring).
1.2 Microservices
Each functional element is placed in an independently replaceable and upgradeable service (the traditional service is split into multiple).
1.3 Dependence
Spring boot extracts all the scene functions into one starters (starters), you only need to introduce scenes in the project, and dependencies are automatically imported.
1.4 File directory
main /
├── java: Write your own java business logic
│ └── xyz
│ └── cqulwj
│ ├── the Controller
│ │ └── HomeController.java
│ └── Main.java is
└── Resources: Resources
├ ── application.properties: Spring Boot application configuration file
├── static: all static resources; js css images, etc.;
└── templates: save all template pages; (using embedded tomcat, JSP pages are not supported by default)
2 Configuration file
2.1 Configuration file
Spring Boot uses a global configuration file with a fixed file name:
- application.properties
- application.yml
yml is a text markup language, data-centric (recommended)
2.2 YAML syntax
K: V identifies a pair of key-value pairs and
indents with spaces to identify hierarchical relationships
server:
port: 8080
address: 0.0.0.0
Object, Map writing
student:{
no:001,name:Jack}
Two ways to write array
pets:
- cat
- dog
- pig
or
pets:[cat,dog,pig]
2.3 yml configuration file value injection
2.3.1 First declare the class to be injected
@Component
@ConfigurationProperties(prefix = "student")
public class Student {
private Integer num;
……;
}
Note that there must be a setter method.
2.3.2 Configure the value of the class in yml
student:
num: 18
……
2.3.3 Configure pom file dependency
<!--yml依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
2.3.4 Use
@Autowired
Student student;
2.4 Configuration file placeholder
Random number: ${random.int}, ${random.value}, ${random.long}
Default value: ${num:10} (the default value after the colon)
2.5 profile
2.5.1 Multiple profile files
The configuration file can be: Application-{profile}.yml/properties
activation: spring.profiles.active=devjijiji
2.5.2 yml supports multiple document blocks
server:
port: 8081
address: 0.0.0.0
spring:
profiles:
active: dev
---
spring:
profiles: dev
server:
port: 8082
Default 8081, activate dev to open port 8082
2.6 Load location of configuration file
- file: ./config/ namely: /config under the project root directory, and pom.xml level
- file: ./ namely: project root directory, and pom.xml level
- classpath:/config/ 即:main/resources/config/application.properties
- classpath:/ 即:main/resources/application.properties
Knowledge points: load according to priority from high to low, and configure the configuration with high coverage ; the configuration file can specify the location separately, and add the parameter-spring.config.location when the command line is started (it is convenient to configure after deployment); also The configuration file can be placed in the same directory as the packaged jar package.
2.7 Principle of automatic configuration
To be dig deeper...
3 logs
3.1 Log frame selection
Log facade: SLF4J
log implementation: Logback
Spring framework uses JCL by default;
Spring Boot uses SLF4J and logback.
3.2 Log framework usage
3.2.1 Using SLF4j
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Controller
public class HomeController {
@ResponseBody
@RequestMapping("/home")
public String home(){
Logger logger= LoggerFactory.getLogger(HomeController.class);
logger.info("进入访问");
return "This is my home!!!";
}
}
3.2.2 Mixed use of log frameworks in different packages
You can use the corresponding intermediate package of slf4j to replace the original log framework, and then use other implementations of slf4j.
3.3 Spring Boot log relationship
3.3.1 The bottom layer is also slf4j+logback;
3.3.2 Springboot also replaces other packages with slf4j;
3.3.3 When a new framework is introduced, its log framework should be removed, and springboot will automatically add slf4j;
3.4 Spring Boot log usage
Logger logger= LoggerFactory.getLogger(HomeController.class)
Log method, the level is from low to high:
trace()<debug()<info()<warn()<error()
3.4.1 The output log level can be set in the configuration file.
logging.level.xyz.cqulwj=trace
3.4.2 Set the location of the log file
logging.file=path/my.log
3.4.3 Specify the log file directory
logging.path=path
3.4.4 You can set your own log configuration file, which
will override the default spring configuration. It is
recommended to name it: logback-spring.xml
4 Spring Boot and Web Development
Use process: select application -> automatic configuration -> write business logic.
4.1 Static resources
4.1.1 To access the resources under webjars, directly give the package directory /webjars/** path.
4.1.2 The resource search path of your own business logic is "/–" (static resource folder):
- “classpath:/META_INF/resources”
- “classpath:/resources/”
- “classpath:/static/”
- “classpath:/public/”
- "/" The root path of the current project
4.1.3 The mapping of the welcome page
All static resource folders + index.html
4.1.4 Icon
All **/favicon.ico are also found under static resources
4.2 Template Engine
JSP、Velocity、Freemarker、Thymeleaf
4.2.1 Introducing thymeleaf
Knowledge points
1 Spring boot only scans the components of the same level and the next level of the SpringBootApplication annotation main class, and will not find the previous level;
2 @ImportResource(locations={"classpath:—.xml"}): Import the Spring configuration file to make the configuration file effective;
3 @PropertySource loads the specified configuration file;
4 @Configuration configuration class -> @Bean adds the method return value to the container