gpt4 book ai didi

java - 如何从 spring-boot 应用程序中的 application.yml 文件读取属性

转载 作者:行者123 更新时间:2023-12-01 17:43:36 25 4
gpt4 key购买 nike

我的应用程序已将嵌套属性存储在 application.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("演示")

类演示:

@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]

最佳答案

当您手动创建属性 POJO 的实例时,Spring 不知道它,并且属性绑定(bind)不会发生。

 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);
}

您可以将 Demo 作为一个 bean,而不是使用 @EnableConfigurationProperties 注释配置,如 Type-safe Configuration Properties 所示。 .

@Component
@ConfigurationProperties("demo")
public class Demo {
...
}

然后你可以从上下文中获取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());
}
}

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

UPD2:回复评论。您可以使用 ObjectMapperDemo bean 构建 JSON:

@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));
}
}

使用 spring-boot-starter-web 不需要额外的依赖项。否则,您可以添加 jackson :

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.0.1</version>
</dependency>

关于java - 如何从 spring-boot 应用程序中的 application.yml 文件读取属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58047778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com