使用 Spring 1.x 访问系统属性

Accessing system properties with Spring 1.x

在Spring4.x中,我总是使用file:#{systemProperties['user.home']}来访问本地文件和加载配置变量。但是,对于一个非常老的项目,我们必须使用 Spring 1.x (1.2.7),同样的代码现在不起作用。我也试过 file:${systemProperties['user.home']},但没有。在我看来,环境无法解析占位符 systemProperties(请参阅错误返回部分)

有人可以给我提示吗?

应用程序上下文提取

<bean id="props" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations">
        <list>
            <value>file:{systemProperties['user.home']}/ldap/conf/ldapconfiguration.properties</value>
        </list>
   </property>
   <property name="ignoreResourceNotFound" value="false" />
</bean>

返回错误

Error creating bean with name 'propertyConfigurer' defined in class path resource [ApplicationContext.xml]: Cannot resolve reference to bean 'props' while setting bean property 'properties'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'props' defined in class path resource [ApplicationContext.xml]: Initialization of bean failed; nested exception is java.io.FileNotFoundException: ${systemProperties['user.home']}\ldap\conf\ldapconfiguration.properties (The system cannot find the path specified)

谢谢。

解决方案 由 Jiri Tousek 提供:

<bean id="props"
    class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations">
        <list>
           <value>file:${user.home}/ldap/conf/ldapconfiguration.properties</value>
        </list>
    </property>
<property name="ignoreResourceNotFound" value="false" />

您可以使用 PropertyPlaceholderConfigurer。它可以支持系统属性,参见#setSystemPropertyMode()