search
HomeJavaLoad list of objects from application yml Java Spring Boot

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. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools