我目前正在使用@Value Spring 3.1.x注释,如下所示:
@Value("${stuff.value:}")
private String value;
Run Code Online (Sandbox Code Playgroud)
如果属性不存在,这会将空字符串放入变量中.我想将null作为默认值而不是空字符串.当然,我还想避免在未设置属性stuff.value时出错.
是否可以使用Spring @Value将属性文件中的值映射到HashMap.
目前我有类似的东西,映射一个值不是问题.但我需要在HashMap过期中映射自定义值.这样的事情可能吗?
@Service
@PropertySource(value = "classpath:my_service.properties")
public class SomeServiceImpl implements SomeService {
@Value("#{conf['service.cache']}")
private final boolean useCache = false;
@Value("#{conf['service.expiration.[<custom name>]']}")
private final HashMap<String, String> expirations = new HashMap<String, String>();
Run Code Online (Sandbox Code Playgroud)
属性文件:'my_service.properties'
service.cache=true
service.expiration.name1=100
service.expiration.name2=20
Run Code Online (Sandbox Code Playgroud)
像这个键映射是否可行:值集
name1 = 100
name2 = 20