The most detailed blog explaining SpringBoot

One: Spring Boot

Spring Boot is a framework built on the basis of Spring, the purpose is to simplify the construction and development process of Spring projects

The community version of Idea needs to install the Spring Assistant plug-in, but the professional version does not.

Professional version of Idea to create the basic process of Spring Boot project

insert image description here
insert image description here
insert image description here
insert image description here

Configure domestic sources and view local jar packages.
insert image description here

insert image description here

insert image description here
Delete useless files in Spring Boot

insert image description here
Start Spring Boot
insert image description here

Two: Spring Boot configuration file

Spring Boot configuration file format:

  1. ×××.properties (default configuration file format)
  2. ×××.yml (new version, improved configuration file format)

Note: The configuration file in properties format has a higher priority than yml. It is recommended that only one format of configuration files be used in a project.

Configuration files in Spring Boot have two important functions

  1. Provide data support for the system framework (system level)
  2. For programmers to use by themselves (user level)

Method 1 for Spring Boot to read configuration: @Value annotation reads a single configuration item

	@Value("${cctalk.token}")  // 读取配置文件
    private String cctalkToken;

Properiters Disadvantage Analysis

There are redundant configuration items, and it is cumbersome to set multiple parameters for an object. It needs to be written from the beginning to the end.

yml syntax
key: value [Note that there must be spaces]

Method 1 for Spring Boot to read configuration: @Value annotation reads a single configuration item

The second way to read configuration files: @ConfigurationProperties reads an entity class

  1. Map a set of objects in the configuration file to a class
@ConfigurationProperties("student")
  1. Use the injection method to inject student in other classes (here is the startup class)
	@Autowired
    private Student student;

configuration collection

# 设置一个 List 集合
mylist1:
  colors:
    - RED
    - GREEN
    - BLACK
# 行内写法
mylist2: {
    
    colors: [RED,GREEN,BLACK]}

The reading of the collection is the same as that of the object, and it is also read using @ConfigurationProperties.

properties VS yml

  1. yml syntax is more concise
  2. yml has better versatility across languages, it not only supports java language but also golang and python
  3. yml supports more data types
  4. The configuration file in yml format is more error-prone when writing, while properties is more traditional and complex in writing, but it is less error-prone

There are three other ways to read configuration files

  1. Use Environment to read configuration files
  2. Use @PropertySource to read configuration files
    to solve the problem of garbled characters: @PropertySource (value="dev.properties", encoding="utf-8") [only supports properties by default]
  3. Read the configuration file using the original sound mode

Three: SpringBoot log files

Implementation steps for developers to customize printing logs:

  1. Get the log object in the program
private static Logger logger = LoggerFactory.getLogger(UserController.class);
  1. Output what to print using the relevant syntax of the log object
logger.info("!!!!!!执行了 say Hi 方法,得到了参数 name:"+name);

insert image description here

Common logging framework

insert image description here
SLF4J log level

  • trace: trace, a little meaning, the lowest level
  • info: general print information
  • debug: Print key information when debugging is required
  • warn: warning, does not affect the use, but needs attention
  • error: error information, higher level error log information
  • fatal: Fatal, because the code exception causes the program to exit the execution event

Set filter log level in configuration file

logging:
  level:
    root: info

The log level will be higher than the info level log output.

log persistence

There are two ways to implement log persistence:

  1. Set the save path of the log: spring boot will generate log files to the corresponding directory according to its own format
  2. Set the save file name of the log: spring boot will save the log according to the file you set

insert image description here
Since you want to get the log file of the current class using SLF4J, you need to add such a line of code in each class. This method is particularly cumbersome.

insert image description here
In order to use an easier way to get the log class and complete the log printing function. Implemented using lombok annotations.

  1. Add lombok framework
  2. @slf4j annotation gets the log object

Here you need to install a plug-in called EditStarters in IDEA. The function of this plug-in is to quickly add framework dependencies in springboot. After the installation is successful, restart IDEA.

After installing the plugin, right-click Generate in pom.xml to add dependencies.
insert image description here
insert image description here

Explanation of lombok principle

insert image description here
lombok also has the functions of Setter and Getter
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/qq_39537400/article/details/123336892