Article Directory
1. What is Spring Boot
Spring is to simplify Java programs, and Spring Boot was born to simplify Spring program development.
The core content of Spring Boot:
1. Quickly add dependencies;
2. Built-in web container;
3. Automatic assembly.
2. Advantages of Spring Boot
(1) Rapid integration framework, Spring Boot provides the function of starting and adding dependencies, which is used to integrate various frameworks in seconds.
(2) Built-in running container, no need to configure web containers such as Tomcat, directly run and deploy programs.
(3) Quickly deploy the project, and the project can be started and run without ordering the external container.
(4) You can completely abandon the cumbersome XML and use annotations and configurations for development.
(5) Support more monitoring indicators, which can better understand the operation of the project.
3. Spring Boot project creation
Before creating Spring Boot: Install the Spring Boot Helper plugin. (For the idea community version)
Search:
After spring boot helper is installed:
Create a project:
Method 1: Create in idea.
Method 2: Create a web page https://start.spring.io/
4. Spring Boot configuration file
1. The configuration file (system configuration file) used by the system, such as the setting of the port number and the configuration of connecting to the database.
2. User-defined configuration files.
Configuration file format: properties/yml (yaml)
Special instructions:
(1) When there are two formats of configuration files in a project, and the same configuration items are set in the two configuration files, but the values are different, then the properties take precedence higher level.
(2) Usually, only one format of configuration file exists in a project.
The properties are configured in Chinese, and garbled characters may appear.
1. yml syntax
(1) Format key: value
(2) Read strings in yml:
(3) Read objects in yml:
(4) Configure list collection:
2. The relationship between properties and yml
(1) Properties is a key-value type file configured in the form of key=value, while yml uses a configuration file in a format similar to json (key: value), where the space after : cannot be omitted.
(2) properties is the early default configuration file format, and there is redundant data. And yml can solve data redundancy very well.
(3) yml is more versatile and supports multiple languages, such as Java, Go, and Python.
(4) yml supports more data types.
3. Multi-system configuration
5. Spring Boot log files
1. Log object
Get the log object. The developer is only responsible for invoking the docking self4j (facade mode).
2. Log level
(1) Quickly filter important logs.
(2) Different environments implement different log levels settings.
trace: trace, a little meaning, the lowest level.
debug: Print key information when debugging is required.
info: common print information (default level).
warn: warning, does not affect the use, but needs attention.
error: error information, higher level error log information.
fatal: Fatal, because the code exception program exits the event of execution.
Note: When the log level is set in the program, the program will only print and set the 相同和大于
log level of the current log level.
Log level settings
Different log levels can be set for different directories.
In the configuration file set:
Two fatal flaws of System.out.println vs. logging
(1) The printing information is incomplete (the event and source of the log are not printed);
(2) The hiding and display of the log printing cannot be realized;
(3) The log printed by System.out.println cannot be persisted.
3. Log persistence
Log persistence (saved to disk, etc.):
(1) Set the save directory of the log;
(2) Set the file name of the log save;
logging:
file:
name:
4. Simpler log output—lombok
(1) Add lombok framework;
(2) Use @Slf4j annotation.
Tools for adding dependencies to old projects (EditStarts)
5. Log function
In addition to finding and locating problems, you can also use logs to realize some practical functions such as recording user login logs, recording system operation logs, and recording program execution time.