我的应用程序将嵌套属性存储在 application.yml 文件中。
我想在应用程序启动时将这些属性映射到POJO 。
应用程序.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
Run Code Online (Sandbox Code Playgroud)
为了实现这一点,我使用以下注释:
@Configuration
@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 + "]";
}
}
Run Code Online (Sandbox Code Playgroud)
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 + "]";
}
}
Run Code Online (Sandbox Code Playgroud)
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 + "]";
}
}
Run Code Online (Sandbox Code Playgroud)
主班
@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");
}
}
Run Code Online (Sandbox Code Playgroud)
但我得到以下 o/p:
步骤 1
名称: 演示 [a=null, b=null]
当您手动创建属性 POJO 的实例时,Spring 不知道它,并且属性绑定不会发生。
SpringApplication app = new SpringApplication(MainApplication.class);
app.run();
System.out.println("step 1");
Demo config = new Demo(); // Not a Spring managed bean!
System.out.println("name: " + config);
}
Run Code Online (Sandbox Code Playgroud)
@EnableConfigurationProperties您可以创建一个 bean,而不是使用 注释配置Demo,如类型安全配置属性中所示。
@Component
@ConfigurationProperties("demo")
public class Demo {
...
}
Run Code Online (Sandbox Code Playgroud)
然后你可以Demo从上下文中获取 bean:
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(MainApplication.class, args);
Demo demo = (Demo) context.getBean("demo");
System.out.println(demo.getName());
}
}
Run Code Online (Sandbox Code Playgroud)
UPD: “a”和“b”之前不能有连字符:
demo:
a:
- type: A
prop1: 1
prop2: 2
proop3: 3
- type: B
prop1: 1
prop2: 2
proop3: 3
b:
- type: B
prop1: 1
prop2: 2
proop3: 3
Run Code Online (Sandbox Code Playgroud)
UPD2:回答评论。Demo您可以使用以下方法从 bean构建 JSON ObjectMapper:
@SpringBootApplication
public class MainApplication {
@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper();
}
public static void main(String[] args) throws JsonProcessingException {
...
ObjectMapper objectMapper = (ObjectMapper) context.getBean("objectMapper");
System.out.println(objectMapper.writeValueAsString(demo));
}
}
Run Code Online (Sandbox Code Playgroud)
不需要spring-boot-starter-web额外的依赖。否则,你可以添加jackson:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.0.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16223 次 |
| 最近记录: |