Use of Spring Boot configuration files
Spring Boot configuration files have two writing formats, one ending with .properties and the other ending with .yaml.
.properties configuration file
When creating a new project, the file has been created for us, but it is an empty file
We can open the file configuration and modify it as follows
generate a random character
#生成一个随机字符
mx.secret=${random.secret}
Get the value in the controller
@RestController
@RequestMapping("/index")
public class IndexController {
@Value(value="${mx.secret}")
private String secret;
@RequestMapping
public String index() {
return "Hello World";
}
@RequestMapping("get")
public Map<String, String> get(@RequestParam String name) {
Map map = new HashMap<>();
map.put("name", name);
map.put("address", "北京丰台");
map.put("secret", secret);
return map;
}
}
Open the browser to visit the test address
write a fixed reference
mx.name=www.example.mx
mx.description=${mx.name} is good
Get this property in the controller
@RestController
@RequestMapping("/index")
public class IndexController {
@Value(value="${mx.secret}")
private String secret;
@Value(value="${mx.description}")
private String description;
@RequestMapping
public String index() {
return "Hello World";
}
@RequestMapping("get")
public Map<String, String> get(@RequestParam String name) {
Map map = new HashMap<>();
map.put("name", name);
map.put("address", "北京丰台");
map.put("secret", secret);
map.put("description", description);
return map;
}
}
Open the browser to visit the test address
Modify port, time format, time zone
Since the default time zone of Spring Boot is set to Los Angeles, USA, it will cause our output time to be wrong
#端口设置
server.port=8090
#日期格式化
spring.jackson.date-format=yyyy-MM-dd HH-mm-ss
#时区
spring.jackson.time-zone=GMT+8
Get properties in controller
@RestController
@RequestMapping("/index")
public class IndexController {
@Value(value="${mx.secret}")
private String secret;
@Value(value="${mx.description}")
private String description;
@RequestMapping
public String index() {
return "Hello World";
}
@RequestMapping("get")
public Map<String, String> get(@RequestParam String name) {
Map map = new HashMap<>();
map.put("name", name);
map.put("address", "北京丰台");
map.put("secret", secret);
map.put("description", description);
return map;
}
@RequestMapping("find/{id}/{name}")
public User find(@PathVariable String id, @PathVariable String name) {
User user = new User();
user.setAge(99);
user.setId(id);
user.setName(name);
user.setDate(new Date());
return user;
}
}
Open the browser to access the test address
and you can see that the port has been modified successfully.
Spring Boot .properties use of multiple configuration files
Add two new configuration files, respectively application-dev.properties and application-test.properties
are configured as follows
#生成一个随机字符
mx.secret=${random.secret}
mx.name=www.example.mx
mx.description=${mx.name} is good
#端口设置
server.port=8095
spring.jackson.date-format=yyyy-MM-dd HH-mm-ss
spring.jackson.time-zone=GMT+8
#生成一个随机字符
mx.secret=${random.secret}
mx.name=www.example.mx
mx.description=${mx.name} is good
#端口设置
server.port=8090
spring.jackson.date-format=yyyy-MM-dd HH-mm-ss
spring.jackson.time-zone=GMT+8
Then modify the application.properties configuration to add the following code
spring.profiles.active=test
Restart the project and find that the configuration in application-test.properties has been used. Similarly, if it is changed to dev, the configuration in application-dev.properties is used.
We can also run parameters in the command line
1. First package the project
2. Right-click to open the command line and enter the command as follows
java -jar .\spring-boot-0.0.1-SNAPSHOT.jar
3. The configuration of application-test.properties is running at this time
4. We can also run the following command
java -jar .\spring-boot-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
5. The configuration of application-dev.properties is running at this time
.yaml configuration file
You can create a new .yaml file yourself as follows
The configuration format is as follows
#生成一个随机字符
mx:
secret: ${random.secret}
name: www.example.mx
description: ${mx.name} is good
#端口设置
server:
port: 8081
spring:
jackson:
date-format: yyyy-MM-dd HH-mm-ss
time-zone: GMT+8
Open the browser to access the test address
The effect is actually the same, .yaml makes the writing more concise
Spring Boot .yaml multi-configuration file usage
Modify the configuration file as follows, .yaml can use multiple configurations in one file, ---
separated by:
#生成一个随机字符
mx:
secret: ${random.secret}
name: www.example.mx
description: ${mx.name} is good
#端口设置
server:
port: 8081
spring:
profiles:
active: test
jackson:
date-format: yyyy-MM-dd HH-mm-ss
time-zone: GMT+8
---
spring:
profiles: test
server:
port: 8082
---
spring:
profiles: dev
server:
port: 8083
Looking at the command line, you can see the configuration that has been used for test