使用Spring Context访问类路径属性和用户定义的属性文件?

时间:2014-03-04 09:22:54

标签: java spring spring-mvc properties spring-3

我试图访问类路径属性&用户定义的路径属性文件如下:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>Common.properties</value>
            <value>user.home:/database.properties</value>
        </list>
      </property> 
      <property name="ignoreUnresolvablePlaceholders" value="true"/>
      <property name="ignoreResourceNotFound" value="false"/>
</bean>

Common.properties路径正在运行,但user.home:/database.properties路径无效。它显示错误:

org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [user.home:/database.properties] cannot be opened because it does not exist
(MSC service thread 1-6) at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:87)

使用jBoss 7并且我更改了standalone.bat以包含以下路径:

set JAVA_OPTS=-Duser.home=file:%JBOSS_CONFIG_DIR%/database.properties

这是对的吗?如果有任何其他方式访问自定义属性路径,请告诉我。

1 个答案:

答案 0 :(得分:0)

尝试使用占位符来保存系统属性键:${user.home}。您还复制了System属性和配置文件中的database.properties