[英]Collection data in Spring Java based configuration
I am working on migrating Spring XML configuration to Java based configuration. 我正在将Spring XML配置迁移到基于Java的配置。 During the process, I run into a situation of collection type data and don't know how to handle it in the Java based configuration.
在此过程中,我遇到了收集类型数据的情况,并且不知道如何在基于Java的配置中处理它。 For example,
例如,
<util:map id="imageDimensions" map-class="java.util.HashMap">
<entry key="regular" value="640x480" />
<entry key="small" value="320x240" />
<entry key="thumb" value="200x150" />
</util:map>
If I don't want to have this map data in Java configuration, what I need to do about it? 如果我不想在Java配置中使用此地图数据,该怎么办?
You have to add bean in your configuration: 您必须在配置中添加bean:
@Bean
public Map<String, String> imageDimensions() {
// create your map
}
If you prefer to have your map data in for example properties file you have to inject Environment field and then by invoking getProperty() retrieve and set all needed elements. 如果您希望将地图数据存储在例如属性文件中,则必须注入Environment字段,然后通过调用getProperty()检索并设置所有需要的元素。
@Configuration
public class AppConfig {
@Inject Environment env;
@Bean
public Map<String, String> imageDimensions() {
Map<String, String> map = new HashMap<>();
// create your map
return map;
}
}
You can also of course read this data from xml or yaml file 您当然也可以从xml或yaml文件中读取此数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.