【发布时间】:2020-01-22 15:47:56
【问题描述】:
我的应用程序具有存储在 applicaion.yml 文件中的嵌套属性。
我想在应用程序启动时将这些属性映射到 POJO。
Application.yml:
demo:
- A:
- type: A
prop1: 1
prop2: 2
proop3: 3
- type: B
prop1: 1
prop2: 2
proop3: 3
- B:
- type: A
prop1: 1
prop2: 2
proop3: 3
- type: B
prop1: 1
prop2: 2
proop3: 3
为了实现这一点,我使用以下注释:
@配置
@EnableConfigurationProperties
@ConfigurationProperties("demo")
课堂演示:
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties("demo")
public class Demo {
@JsonProperty("A")
private List<A> a = null;
@JsonProperty("B")
private List<B> b = null;
@JsonProperty("A")
public List<A> getA() {
return a;
}
@JsonProperty("A")
public void setA(List<A> a) {
this.a = a;
}
@JsonProperty("B")
public List<B> getB() {
return b;
}
@JsonProperty("B")
public void setB(List<B> b) {
this.b = b;
}
@Override
public String toString() {
return "Demo [a=" + a + ", b=" + b + "]";
}
}
A类:
public class A {
@JsonProperty("type")
private String type;
@JsonProperty("prop1")
private Integer prop1;
@JsonProperty("prop2")
private Integer prop2;
@JsonProperty("proop3")
private Integer proop3;
@JsonProperty("type")
public String getType() {
return type;
}
@JsonProperty("type")
public void setType(String type) {
this.type = type;
}
@JsonProperty("prop1")
public Integer getProp1() {
return prop1;
}
@JsonProperty("prop1")
public void setProp1(Integer prop1) {
this.prop1 = prop1;
}
@JsonProperty("prop2")
public Integer getProp2() {
return prop2;
}
@JsonProperty("prop2")
public void setProp2(Integer prop2) {
this.prop2 = prop2;
}
@JsonProperty("proop3")
public Integer getProop3() {
return proop3;
}
@JsonProperty("proop3")
public void setProop3(Integer proop3) {
this.proop3 = proop3;
}
@Override
public String toString() {
return "A [type=" + type + ", prop1=" + prop1 + ", prop2=" + prop2 + ", proop3=" + proop3 + "]";
}
}
B类
public class B {
@JsonProperty("type")
private String type;
@JsonProperty("prop1")
private Integer prop1;
@JsonProperty("prop2")
private Integer prop2;
@JsonProperty("proop3")
private Integer proop3;
@JsonProperty("type")
public String getType() {
return type;
}
@JsonProperty("type")
public void setType(String type) {
this.type = type;
}
@JsonProperty("prop1")
public Integer getProp1() {
return prop1;
}
@JsonProperty("prop1")
public void setProp1(Integer prop1) {
this.prop1 = prop1;
}
@JsonProperty("prop2")
public Integer getProp2() {
return prop2;
}
@JsonProperty("prop2")
public void setProp2(Integer prop2) {
this.prop2 = prop2;
}
@JsonProperty("proop3")
public Integer getProop3() {
return proop3;
}
@JsonProperty("proop3")
public void setProop3(Integer proop3) {
this.proop3 = proop3;
}
@Override
public String toString() {
return "B [type=" + type + ", prop1=" + prop1 + ", prop2=" + prop2 + ", proop3=" + proop3 + "]";
}
}
主类
@SpringBootApplication
@ComponentScan(basePackages = {"com.microservice.*"})
@EnableJpaRepositories("com.microservice.*")
@EntityScan("com.microservice.*")
public class MainApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MainApplication.class);
app.run();
System.out.println("step 1");
Demo config = new Demo();
System.out.println("name: " + config);
}
public void run(String... args) throws Exception {
System.out.println("step 2");
}
}
但我低于 o/p:
第 1 步
名称:演示 [a=null, b=null]
【问题讨论】:
-
您正在使用的演示类中有什么?
-
Demo 类包含 List 和 List。 A & B 是类。
标签: java spring-boot