一、获取自定义配置
appilication.properties 添加
app.name = spring Boot
通过注解@Value获取
@Value("${app.name}")
private String appName;
二、自定义配置映射到对象
application.properties
配置必须有前缀,且前缀是一样的,才能用一个配置类
app.name = spring Boot app.version = 2.6
创建配置类文件App .java
package com.example.demo.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component // 将此类给spring容器进行管理
@ConfigurationProperties(prefix = "app")
public class App {
private String name;
private double version;
public String getName() {
return name;
}
public String setName(String name) {
return this.name = name;
}
public double getVersion() {
return version;
}
public double setVersion(Double version) {
return this.version = version;
}
}
控制器:
@Autowired
private App app;
@RequestMapping(value = "/")
public @ResponseBody String say() {
return "Hello " + app.getName() + " " + app.getVersion();
}



