Home > Java > Load list of objects from application yml Java Spring Boot

Load list of objects from application yml Java Spring Boot

WBOY
Release: 2024-02-22 13:16:05
forward
1140 people have browsed it

php editor Xigua will take you to delve into the issue of how application yml loads object lists in Java. In Java Spring Boot, by properly configuring the yml file, you can load the object list and flexibly apply it in the application. Next, let us master this technique together to improve the practicality and efficiency in Spring Boot development.

Question content

I have this configuration class:

@configuration
@configurationproperties(prefix = "house")
public class projectconfig{

    private list<housetemplate> templates;

    // getters and setters
}
Copy after login

housetemplate Class:

public class housetemplate{

    private string id;
    private string description;

    // getters and setters
}
Copy after login

This is my application-test.yml

house:
  templates:
    -
      id: "colonial"
      description: "colonial house template"
    -
      id: "cottage"
      description: "cottage house template"
    -
      id: "apartment"
      description: "apartment house template"


examplestring: hello
Copy after login

In my test class I have the following:

@runwith(springrunner.class)
@enableconfigurationproperties(value = projectconfig.class)
@testpropertysource("classpath:application-test.yml")
public class yamlapplicationcontextloadingspec {

    @value("${examplestring}")
    string example;

    @autowired
    projectconfig projectconfig;


    @test
    public void exampleshouldcontainhello(){
        assertthat(example).isequaltoignoringcase("hello");
    }
   
    @test
    public void appcontextcontainshousetemplates(){
        list<housetemplate> housetemplates = projectconfig.gettemplates();
        assertthat(housetemplates).isnotnull();
    }
}
Copy after login

The first test for examplestring passes, but the second test fails. Why can't I map yml into housetemplate list?

edit

<artifactId>spring-core</artifactId>
<version>4.3.6.RELEASE</version>

<artifactId>spring-boot</artifactId>
<version>1.5.1.RELEASE</version>

<artifactId>junit</artifactId>
<version>4.12</version>
Copy after login

I know they are really old and I would love to upgrade but I can't...this is what I have to deal with.

Solution

Use @configurationpropertiesAfter reading from yml, it can be registered as a bean, which can then be obtained through applicationcontext.

@configuration
@configurationproperties(prefix = "house")
public class testconfig {

    private list<housetemplate> templates;

    @bean
    public list<housetemplate> templates() {
        return templates;
    }

    // getter and setter
}

@restcontroller
@requestmapping("/api/test")
public class testcontroller {

    @autowired
    private applicationcontext applicationcontext;

    @getmapping("get")
    @suppresswarnings("unchecked")
    public list<housetemplate> get() {
        return (list<housetemplate>) applicationcontext.getbean("templates");
    }
}
Copy after login

If you don't want to register as a bean, you can also declare the variable as static.

This is an example.

@Configuration
@ConfigurationProperties(prefix = "House")
public class TestConfig {

    private static List<HouseTemplate> templates;

    public static List<HouseTemplate> get() {
        return templates;
    }

    public List<HouseTemplate> getTemplates() {
        return templates;
    }

    public void setTemplates(List<HouseTemplate> templates) {
        TestConfig.templates = templates;
    }
}

@RestController
@RequestMapping("/api/test")
public class TestController {

    @GetMapping("get")
    public List<HouseTemplate> get() {
        return TestConfig.get();
    }
}
Copy after login

The above is the detailed content of Load list of objects from application yml Java Spring Boot. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template