有人可以提供一些想法来注入属性文件中的所有动态键和值并将其传递为Map
to DBConstants
使用 Setter 注入和 Collection 的类。
密钥事先未知并且可能会有所不同。
// Example Property File that stores all db related details
// db.properties
db.username.admin=root
db.password.admin=password12
db.username.user=admin
db.password.user=password13
DBConstants
包含需要注入所有键和值的映射 dbConstants。
请提供 bean 定义以将所有键和值注入到 Map dbConstants。
public class DBConstants {
private Map<String,String> dbConstants;
public Map<String, String> getDbConstants() {
return dbConstants;
}
public void setDbConstants(Map<String, String> dbConstants) {
this.dbConstants = dbConstants;
}
}
您可以使用属性文件创建 PropertiesFactoryBean,然后使用 @Resource 注释将其注入您想要将其用作地图的位置。
@Bean(name = "myProperties")
public static PropertiesFactoryBean mapper() {
PropertiesFactoryBean bean = new PropertiesFactoryBean();
bean.setLocation(new ClassPathResource("prop_file_name.properties"));
return bean;
}
Usage:
@Resource(name = "myProperties")
private Map<String, String> myProperties;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)