SpringBoot使用@ConfigurationProperties注解绑定配置文件
简单属性注入
@Component
@ConfigurationProperties(prefix = "test1")
public class ConfigurationTest1 {
private String username;
private String email;
// getter setter toString
test1:
username: Dima
email: 123456789@qq.com
List集合
@Component
@ConfigurationProperties(prefix = "test2")
public class ConfigurationTest2 {
private List<String> list = new ArrayList<>();
// getter setter toString
test2:
list[0]: zhangsan
list[1]: lisi
使用
@RestController
public class TestController {
@Autowired
private ConfigurationTest1 configurationTest1;
@Autowired
private ConfigurationTest2 configurationTest2;
@GetMapping(value = "/test")
public String test() {
System.out.println(configurationTest1);
System.out.println(configurationTest2);
return "Hello SpringBoot!";
}
}
效果
ConfigurationTest1{username='Dima', email='123456789@qq.com'}
ConfigurationTest2{list=[zhangsan, lisi]}