java 将 YAML 列表映射到 Spring Boot 中的对象列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36232138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mapping YAML List to List of Objects in Spring Boot
提问by Brandon E Taylor
I have a problem similar to that described in Mapping list in Yaml to list of objects in Spring Bootexcept 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.
我有一个类似于将Yaml 中的列表映射到 Spring Boot 中的对象列表中描述的问题,只是我想将对象中至少一个字段的标识符与 YAML 中使用的相应键名不同。
For example:
例如:
YAML File:
YAML 文件:
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).
我希望 @Value 注释能让我注入相应的属性值,但这似乎不起作用(注入“id”字段似乎工作得很好)。
Is there a way to do this with @Value (or any other annotation)?
有没有办法用@Value(或任何其他注释)来做到这一点?
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 - e.g. 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.
编辑:请注意,我希望确定是否可以在不更改. 我可能想要这样做的原因有很多 - 例如,我可能无法控制 YAML 文件的格式,并且我想在我的 POJO 中使用比 YAML 文件作者使用的更具描述性的标识符名称。
回答by luboskrnac
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:
正如 Stephave Nicoll 所提到的,@Value
注解与@ConfigurationProperties
. 只需将内部 POJO 中的字段命名为与配置文件中相同的字段,这应该可以工作:
@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:
使用普通的 Spring/Spring Boot,我认为您无法映射具有不同名称的字段并将其加载到网关列表中。可以选择使用普通的 @Value 注释,但您的网关计数需要进行硬编码:
@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
// ...
}