This article brings you a detailed introduction (code example) about SpringBoot2 configuration. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1.Properties and Yaml
SpringBoot supports two configuration files, properties and yaml application.properties/application.yml
Yaml is simple to use
1.Introduction to yaml
yaml is data-centric and is more suitable for configuration files than json and xml
2.yaml basic syntax
k: v: A key-value pair in the form, : must be followed by a space
Use spaces to indent to control hierarchical relationships
Case sensitive
Example:
server: port: 8081 # 设置默认端口号 servlet: path: /init
k: v Write directly
[No single or double quotes are required for strings by default]
"": double quotes; special characters in the string will not be escaped;
'': Single quotes; special characters will be escaped, and the special characters will end up being just ordinary string data
k: v: Write the object on the next line The relationship between attributes and values
For example:
persion: name: zhangsan age: 10
Inline writing:
persion: {name: zhangsan,age: 10}
Ordinary array, List, Set uses - value to represent an element in the array
arrays: - arrays1 - arrays2 - arrays3
Map uses the form of k: v to represent
map: k1: v1 k2: v2 k3: v3
Introduce dependencies in pom.xml , used by the annotation processor to generate its own metadata
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
Entity class:
/** * persion类 */ @Component @ConfigurationProperties(prefix = "persion") public class Persion { private String name; private int age; private boolean isMan; private Date birth; private String[] hobbys; private Map<String,String> skills; private List<String> enjoys; // 省略getter setter toString }
yaml configuration:
persion: name: zhangsan age: 20 isMan: true birth: 1997/11/12 hobbys: - 篮球 - 足球 - 乒乓球 skills: java: javase python: ai enjoys: - eating - running - playgames
Test class
@Autowired Persion persion; @Test public void contextLoads() { System.out.println(persion); }
2. Server configuration
Change port:
server.port=8081
Change context access path, SpringBoot default sample path is "/":
server.servlet.context-path=/base
Common server configuration
server.port:SpringBoot监听端口 server.error.path:错误处理路径 server.servlet.context-path:配置SpringBoot默认上下文路径
Three log configuration
Define log level
Log level ERROR WARN DEBUF INFO TRACE level from high to low
# 输出日志级别 ERROR WARN DEBUF INFO TRACE,日志只会打印当前级别,以及高于当前级别的日志 logging.level.root=info
Output log to file
# 输出日志到文件 logging.file=/my.log
Define log Output format
Log format output to the console:
logging.pattern.console=【%level %date %logger %thread %M %L %m】 %n
Log format output to the log file:
# 定义输出到文件的日志格式 logging.pattern.file=【%level %date %logger %thread %M %L %m】 %n
Basic parameters of log format
属性 内容 %level 表示输出日志级别 %date 表示日期发生时的时间 %logger 用于输出Logger名字,包名+类名,{n}限定输出长度 %thread 当前线程名 %M 日志发生时方法的名字 %L 日志调用所在代码行 %m 日志消息 %n 日志换行
The above is the detailed content of Detailed introduction to SpringBoot2 configuration (code example). For more information, please follow other related articles on the PHP Chinese website!