具体可参考:
URL:https://hello.blog.csdn.net/article/details/104575745
如下是一种使用场景
当Bean被实例化时,@ConfigurationProperties会将对应前缀的后面的属性与Bean对象的属性匹配。符合条件则进行赋值。
配置文件内内容
test:
test1: 'test1'
test2:
test3:
test4: 'test4'
test5: 'test5'
test6:
test7: 'test7'
ConfigurationProperties 使用时赋值用的对象实体类
package net.shop.test;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
//将普通pojo实例化到spring容器
@Component
//表示将配置文件中 前缀为test 的内容复制到改对象上
@ConfigurationProperties(prefix = "test")
public class CompanyProperties implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
}
private String test1;
private test2 test2 = new test2();
private test6 test6 = new test6();
public class test2 {
private String test5;
private test3 test3 = new test3();
public class test3 {
private String test4;
public String getTest4() {
return test4;
}
public void setTest4(String test4) {
this.test4 = test4;
}
}
public CompanyProperties.test2.test3 getTest3() {
return test3;
}
public void setTest3(CompanyProperties.test2.test3 test3) {
this.test3 = test3;
}
public String getTest5() {
return test5;
}
public void setTest5(String test5) {
this.test5 = test5;
}
}
public class test6 {
private String test7;
public String getTest7() {
return test7;
}
public void setTest7(String test7) {
this.test7 = test7;
}
}
public String getTest1() {
return test1;
}
public void setTest1(String test1) {
this.test1 = test1;
}
public CompanyProperties.test2 getTest2() {
return test2;
}
public void setTest2(CompanyProperties.test2 test2) {
this.test2 = test2;
}
public CompanyProperties.test6 getTest6() {
return test6;
}
public void setTest6(CompanyProperties.test6 test6) {
this.test6 = test6;
}
}
使用方式
@Autowired
private CompanyProperties props;
public void centerOrderImporting(){
System.out.println(props.getTest1());
System.out.println(props.getTest2().getTest3().getTest4());
System.out.println(props.getTest2().getTest5());
System.out.println(props.getTest6().getTest7());
}
输出内容: