【发布时间】:2016-09-29 09:55:17
【问题描述】:
我遇到了无法从我的 Java 代码访问 Spring 属性的问题。
这里是上下文:
<context:property-placeholder ignore-resource-not-found="false"
location="file:/${setup.properties.directory}/setup.properties"/>
setup.properties 文件如下所示:
paymentProvider.x.url=x
代码是:
SpringContext.INSTANCE.getEnvironment()
.getProperty("paymentProvider.x.url");
运行过程中没有错误。但是,上面代码的结果是null。
有人知道为什么吗?
【问题讨论】:
-
你用的是什么spring版本?能分享一下你的pom.xml吗?如果属性文件在你的类路径中,请点击链接mkyong.com/spring/spring-propertysources-example
-
@VelNaga, 4.3.1.RELEASE.
-
您是否检查了@Value 注释获取属性的方式?您是否在类路径或静态路径中有此属性?
-
也可以点击此链接baeldung.com/2012/02/06/properties-with-spring 使用不同的选项加载属性。
标签: java spring properties