2016-08-01 15 views
0

在Spring 4.x中,我總是使用file:#{systemProperties['user.home']}來訪問本地文件並加載配置變量。但是,對於一個非常古老的項目,我們必須使用Spring 1.x(1.2.7),現在相同的代碼不起作用。我也試過file:${systemProperties['user.home']},但沒有。在我看來,環境不能解決佔位符systemProperties(看錯誤返回部分)使用Spring 1.x訪問系統屬性

有人可以給我一個提示嗎?

應用程序上下文提取

<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) 

謝謝。

解決方案 正如吉日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" /> 

回答

1

您可以使用PropertyPlaceholderConfigurer。它可以支持系統屬性,請參閱#setSystemPropertyMode()

+0

它的工作原理!非常感謝你!! – gioconno