SpringBoot获取配置文件的自定义参数

1、在application.properties中自定义参数

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root

spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
#开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false

name=cppdy

2、在UserController中获取自定义参数,并创建测试方法

//获取自定义参数的值
@Value("${name}")
private String name;
//测试获取自定义参数值的方法
@RequestMapping("getName")
public String getName() {
    return name;
}
原文地址:https://www.cnblogs.com/jiefu/p/10051979.html