java - spring systemProperties key-IllegalArgumentException : KEY must not be null

标签 java spring annotations

我的 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/

相关文章:

java - 验证 jar 文件 : jar not loaded servlet

iPhone SDK : MapKit multiple custom annotations

java - Jersey 注释的注释处理器

java - 是否可以使用 Hibernate 将一个 Java 对象映射到具有 FK 关系的多个表?

java - Javers 比较 String null 和 ""空字符串

spring - 为嵌入式 tomcat 指定自定义 web.xml

java - 使用 LoadTimeWeaving 的 Couchbase 的 Spring 缓存 - 奇怪的是不工作

java - 更改 json 对象值

java - 将非插件项目引用到 Eclipse 插件项目

java - 从手工制作的 hibernate 映射文件转向注释是否值得?