我的 appContext.xml 中有这个
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>file:pathTo/service.properties</value>
<value>file:pathTo/configuration.properties</value>
</list>
</property>
</bean>
我正在设置一个字符串
@Value("${myServiceKey}")
private String url;
这样就可以了,我得到了 url 中 myServiceKey 的值。
但是当 myServiceKey 不存在时我想使用默认值,所以我尝试了这个
@Value("${myServiceKey:defaultValue}")
private String url;
并且它总是设置“defaultValue”而不是正确的“myServiceKey”。
我还意识到使用这个:
@Value("#{systemProperties['myServiceKey']}")
private String url;
我有一个异常(exception)
WARN MSF4JMessageProcessor:262 - Unmapped exception -java.lang.IllegalArgumentException: URI must not be null
有关系吗?怎么了??
我使用的是 spring 版本 4.3.9.RELEASE
提前致谢。
最佳答案
最后我发现了问题,那就是我在PropertyPlaceholderConfigurer中有多个属性。 我发现它在:https://jira.spring.io/browse/SPR-9989
这里建议的解决方案对我有用 Spring Boot : Spring always assigns default value to property despite of it being present in .properties file
我将 PropertyPlaceholderConfigurers 分成两个不同的配置,并向其中一个添加属性 valueSeparator
<property name="valueSeparator" value="="/>
然后我设置默认值,如下所示:
@Value("${myServiceKey= H3ll0W0rld}")
现在可以了。希望它对其他人有帮助。
关于java - spring systemProperties key-IllegalArgumentException : KEY must not be null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48653524/