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

Load list of objects from application yml Java Spring Boot

WBOY
WBOYforward
2024-02-22 13:16:051124browse

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
}

housetemplate Class:

public class housetemplate{

    private string id;
    private string description;

    // getters and setters
}

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

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

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>

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

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

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!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete