简体   繁体   中英

Mapping YAML List to List of Objects in Spring Boot

I have a problem similar to that described in Mapping list in Yaml to list of objects in Spring Boot except that I would like to vary the identifier of least one of the fields in my object from the corresponding key name used in YAML.

For example:

YAML File:

config:
    gateways:
        -
            id: 'g0'
            nbrInputs: 128
            nbrOutputs: 128
        -
            id: 'g1'
            nbrInputs: 128
            nbrOutputs: 128

Configuration Class:

@Configuration
@ConfigurationProperties(prefix="config")
public class GatewayConfig
{
    List<Gateway> gateways = new ArrayList<Gateway>();

    // Getter/Setter for gateways
    // ...

    public static class Gateway
    {
        private String id;

        @Value("${nbrInputs}")
        private int numInputs;

        @Value("${nbrOutputs}")
        private int numOutputs;

        // Getters and Setters
        // ...
    }
}

I was hoping that the @Value annotations would allow me to inject the corresponding property values, but this does not seem to work (injection of the 'id' field seems to work just fine).

Is there a way to do this with @Value (or any other annotation)?

Thank you.


Edit: Note that I am looking to determine whether I can force a correspondence between a YAML property and a field in the inner POJO without changing the name of either . There are several reasons why I might want to do this - eg I might not control the format of the YAML file and I would like to use a more descriptive identifier name in my POJO than was used by the author of the YAML file.

As Stephave Nicoll mentioned, @Value annotation has nothing to do with @ConfigurationProperties . Just name fields in the inner POJO same as in configuration file and this should work:

@Configuration
@ConfigurationProperties(prefix="config")
@EnableConfigurationProperties
public class GatewayConfig
{
    List<Gateway> gateways = new ArrayList<Gateway>();

    // Getter/Setter for gateways
    // ...

    public static class Gateway
    {
        private String id;
        private int nbrInputs;
        private int nbrOutputs;

        // Getters and Setters
        // ...
    }
}

Reaction on comment:

With plain Spring/Spring Boot, I don't think you can map fields with different names and load it to the list of gateways. There would be option to use plain @Value annotation, but your gateway count would need to be hard-coded:

@Component
public class Gateway0{
    @Value("${config.gateways[0].id}")
    private String id;

    @Value("${config.gateways[0].nbrInputs}")
    private int numInputs;

    @Value("${config.gateways[0].nbrOutputs}")
    private int numOutputs;

    // Getters and Setters
    // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM