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

我的应用程序将嵌套属性存储在 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

为了实现这一点,我使用以下注释:


@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 + "]";

    }   

}


胡说叔叔
浏览 160回答 4
4回答

暮色呼如

当您手动创建属性 POJO 的实例时,Spring 不知道它,并且属性绑定不会发生。&nbsp;SpringApplication app = new SpringApplication(MainApplication.class);&nbsp; &nbsp; app.run();&nbsp; &nbsp; System.out.println("step 1");&nbsp; &nbsp; Demo config = new Demo(); // Not a Spring managed bean!&nbsp; &nbsp; System.out.println("name: " + config);}@EnableConfigurationProperties您可以创建一个 bean,而不是使用 注释配置Demo,如类型安全配置属性中所示。@Component@ConfigurationProperties("demo")public class Demo {&nbsp; &nbsp; ...}然后你可以Demo从上下文中获取 bean:@SpringBootApplicationpublic class MainApplication {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; ConfigurableApplicationContext context = SpringApplication.run(MainApplication.class, args);&nbsp; &nbsp; &nbsp; &nbsp; Demo demo = (Demo) context.getBean("demo");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(demo.getName());&nbsp; &nbsp; }}UPD: “a”和“b”之前不能有连字符:demo:&nbsp; a:&nbsp; &nbsp; - type: A&nbsp; &nbsp; &nbsp; prop1: 1&nbsp; &nbsp; &nbsp; prop2: 2&nbsp; &nbsp; &nbsp; proop3: 3&nbsp; &nbsp; - type: B&nbsp; &nbsp; &nbsp; prop1: 1&nbsp; &nbsp; &nbsp; prop2: 2&nbsp; &nbsp; &nbsp; proop3: 3&nbsp; b:&nbsp; &nbsp; - type: B&nbsp; &nbsp; &nbsp; prop1: 1&nbsp; &nbsp; &nbsp; prop2: 2&nbsp; &nbsp; &nbsp; proop3: 3UPD2:回答评论。Demo您可以使用以下方法从 bean构建 JSON ObjectMapper:@SpringBootApplicationpublic class MainApplication {&nbsp; &nbsp; @Bean&nbsp; &nbsp; public ObjectMapper objectMapper() {&nbsp; &nbsp; &nbsp; &nbsp; return new ObjectMapper();&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) throws JsonProcessingException {&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; ObjectMapper objectMapper = (ObjectMapper) context.getBean("objectMapper");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(objectMapper.writeValueAsString(demo));&nbsp; &nbsp; }}不需要spring-boot-starter-web额外的依赖。否则,你可以添加jackson:<dependency>&nbsp; &nbsp; <groupId>com.fasterxml.jackson.core</groupId>&nbsp; &nbsp; <artifactId>jackson-databind</artifactId>&nbsp; &nbsp; <version>2.0.1</version></dependency>

慕码人2483693

您的文件中的内容不正确。您yml可以在此处阅读链接上的Merging YAML lists参考。我写了一个演示,它可以工作。demo:&nbsp; &nbsp; &nbsp; &nbsp; a:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prop1: prop1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prop2: prop2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; blist:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - prop1: prop1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prop2: prop2&nbsp; &nbsp; &nbsp; &nbsp; alist:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - b:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prop1: prop1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prop2: prop2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; blist:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - prop1: prop1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prop2: prop2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - b:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prop1: prop1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prop2: prop2``@ConfigurationProperties(prefix = "demo")public class Demo {&nbsp; &nbsp; private A a;&nbsp; &nbsp; private List<A> alist;&nbsp; &nbsp; // omitted getter/setter}``public class A {&nbsp; &nbsp; private B b;&nbsp; &nbsp; private List<B> blist;&nbsp; &nbsp; // omitted getter/setter}``public class B {&nbsp; &nbsp; private String prop1;&nbsp; &nbsp; private String prop2;&nbsp; &nbsp; // omitted getter/setter}

慕哥9229398

如果您想从.yml或.properties文件读取属性,我建议您创建一个类,可以PropertiesBooter在其中保存从这些文件检索到的所有值。要从属性文件中检索值,您可以编写@Value("${value}")private String&nbsp;

炎炎设计

从 Mikhail 的回答中,只需使用 Jackson ObjectMapper 编写 json,即可获得 json 格式:public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; ConfigurableApplicationContext context = SpringApplication.run(YamlTestApplication.class, args);&nbsp; &nbsp; &nbsp; &nbsp; Demo demo = (Demo) context.getBean("demo");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("name: " + demo);&nbsp; &nbsp; &nbsp; &nbsp; ObjectMapper mapper = new ObjectMapper();&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String test = mapper.writeValueAsString(demo);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("json: "+test);&nbsp; &nbsp; &nbsp; &nbsp; } catch (JsonProcessingException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }输出:name: Demo [a=[A [type=A, prop1=1, prop2=2, proop3=3], A [type=B, prop1=1, prop2=2, proop3=3]], b=[B [type=A, prop1=1, prop2=2, proop3=3], B [type=B, prop1=1, prop2=2, proop3=3]]]json: {"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}]}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java