- 先在pom文件中添加相关依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
- 直接使用@ConfigurationProperties和@Component注解,即可获取到数据
com:
a:
username: admin
password: 123456
@ConfigurationProperties(prefix = "com.a.b")
@Data
@Component
public class BVo {
private String username;
private String password;
}
@Autowired
private BVo bVo;
@GetMapping("/config")
public String getConfig() {
return bVo.getUsername();
}