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
Configure domestic sources and view local jar packages.
Delete useless files in Spring Boot
Start Spring Boot
Two: Spring Boot configuration file
Spring Boot configuration file format:
- ×××.properties (default configuration file format)
- ×××.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
- Provide data support for the system framework (system level)
- 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
- Map a set of objects in the configuration file to a class
@ConfigurationProperties("student")
- 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
- yml syntax is more concise
- yml has better versatility across languages, it not only supports java language but also golang and python
- yml supports more data types
- 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
- Use Environment to read configuration files
- 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] - Read the configuration file using the original sound mode
Three: SpringBoot log files
Implementation steps for developers to customize printing logs:
- Get the log object in the program
private static Logger logger = LoggerFactory.getLogger(UserController.class);
- Output what to print using the relevant syntax of the log object
logger.info("!!!!!!执行了 say Hi 方法,得到了参数 name:"+name);
Common logging framework
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:
- Set the save path of the log: spring boot will generate log files to the corresponding directory according to its own format
- Set the save file name of the log: spring boot will save the log according to the file you set
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.
In order to use an easier way to get the log class and complete the log printing function. Implemented using lombok annotations.
- Add lombok framework
- @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.
Explanation of lombok principle
lombok also has the functions of Setter and Getter