Home > Java > javaTutorial > Detailed introduction to SpringBoot2 configuration (code example)

Detailed introduction to SpringBoot2 configuration (code example)

不言
Release: 2019-02-19 15:56:01
forward
2431 people have browsed it

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
Copy after login

How to write the value

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

Object

k: v: Write the object on the next line The relationship between attributes and values ​​

For example:

 persion:
          name: zhangsan
          age: 10
Copy after login

Inline writing:

persion: {name: zhangsan,age: 10}
Copy after login

Array (List, Set, Map)

Ordinary array, List, Set uses - value to represent an element in the array

arrays:
  - arrays1 
  - arrays2  
  - arrays3
Copy after login

Map uses the form of k: v to represent

map:
  k1: v1
  k2: v2
  k3: v3
Copy after login

Use configuration file injection

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>
Copy after login

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
}
Copy after login

yaml configuration:

persion:
  name: zhangsan
  age: 20
  isMan: true
  birth: 1997/11/12
  hobbys:
    - 篮球
    - 足球
    - 乒乓球
  skills:
    java: javase
    python: ai
  enjoys:
    - eating
    - running
    - playgames
Copy after login

Test class

@Autowired
    Persion persion;

    @Test
    public void contextLoads() {
        System.out.println(persion);
    }
Copy after login

2. Server configuration

Change port:

server.port=8081
Copy after login

Change context access path, SpringBoot default sample path is "/":

server.servlet.context-path=/base
Copy after login

Common server configuration

server.port:SpringBoot监听端口
server.error.path:错误处理路径
server.servlet.context-path:配置SpringBoot默认上下文路径
Copy after login

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
Copy after login

Output log to file

# 输出日志到文件
logging.file=/my.log
Copy after login

Define log Output format
Log format output to the console:

logging.pattern.console=【%level %date %logger %thread %M %L %m】 %n
Copy after login

Log format output to the log file:

# 定义输出到文件的日志格式
logging.pattern.file=【%level %date %logger %thread %M %L %m】 %n
Copy after login

Basic parameters of log format

属性    内容
%level    表示输出日志级别
%date    表示日期发生时的时间
%logger    用于输出Logger名字,包名+类名,{n}限定输出长度
%thread    当前线程名
%M    日志发生时方法的名字
%L    日志调用所在代码行
%m    日志消息
%n    日志换行
Copy after login

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!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template