2017-07-04 87 views
0

我已經配置類似下面春天ignoreResourceNotFound不工作

<bean id="applicationHostProperties" 
     class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
     <property name="ignoreResourceNotFound" value="true"></property> 
     <property name="locations"> 
      <list> 
       <value>classpath:applicationHost.properties</value> 
      </list> 
     </property> 
</bean> 

從我的Spring bean我注入屬性文件

@Value("#{appsDeployedProperties}") 
private Properties appsDeployedProperties; 

既然我已經設置ignoreResourceNotFound =真屬性文件,如果找不到屬性文件,我不期待任何錯誤。

但是我的春天上下文初始化失敗,如果屬性文件丟失。我在這裏做錯了什麼?謝謝你的幫助。

錯誤堆棧跟蹤:

錯誤名爲 '上instanceConfigurati ' 創建豆:不滿意依賴通過現場 'appsDeployedProperties' 表示:前 PRESSION解析失敗;嵌套的異常是org.springframework.expression.spel .SpelEvaluationException:EL1008E:(pos 0):屬性或字段'appsDeployedProper ties'無法在類型爲'org.springframework.beans.factory.confi的對象上找到 g.BeanExpressionContext ' - 也許不公開?嵌套異常是org.springfram ework.beans.factory.BeanExpressionException:表達式解析失敗;嵌套e xception is org.springframework.expression.spel.SpelEvaluationException:EL1008E :(pos 0):無法在對象上找到屬性或字段'appsDeployedProperties' f type'org.springframework.beans.factory.config.BeanExpressionContext ' - 也許 不公開?嵌套的異常是org.springframework.beans.factory.UnsatisfiedDe pendencyException:創建名爲'instanceConfiguration'的bean時出錯:Unsati 通過字段'appsDeployedProperties'表示的已滿依賴:表達式pa 解析失敗;嵌套異常是org.springframework.expression.spel.SpelEvalua tionException:EL1008E:(pos 0):屬性或字段'appsDeployedProperties'canno 在類型'org.springframework.beans.factory.config.BeanExpre ssionContext' - 可能不公開?嵌套異常是org.springframework.beans .factory.BeanExpressionException:表達式解析失敗;嵌套的異常是 org.springframework.expression.spel.SpelEvaluationException:EL1008E:(pos 0):P roperty或字段'appsDeployedProperties'無法在'org類型的對象上找到' .springframework.beans.factory.config.BeanExpressionContext' - 可能不公開?在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanP ostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.j ava:569)〜[spring-beans-4.3.2.RELEASE.jar!/:4.3 .2.RELEASE] at org.springframework.beans.factory.annotation.InjectionMetadata.inject (InjectionMetadata.java:88)〜[spring-beans-4.3.2.RELEASE.jar!/:4.3.2.RELEASE] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanP 個ostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java :349)〜[彈簧豆-4.3.2.RELEASE.jar /:4.3.2.RELEASE]

+0

添加堆棧跟蹤,請 – Jens

+0

添加了錯誤的堆棧跟蹤 – lives

+0

可以請你重新設置堆棧跟蹤,它不是以這種形式可讀 – Jens

回答

0

你必須從applicationHost.properties結合鍵爲String,而比Properties。例如,如果你在applicationHost.properties關鍵appsDeployedProperties,如下:

applicationHost.properties

appsDeployedProperties=hello 

在Spring Bean時,你必須爲字符串使用,如:

@Value("#{appsDeployedProperties}") 
private String appsDeployedProperties; 

PS:你可以添加ignoreUnresolvablePlaceholders鍵值爲true告訴Spring忽略bean中使用的任何屬性文件中沒有提到的鍵值,如下所示:

<bean id="applicationHostProperties" 
     class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
     <property name="ignoreResourceNotFound" value="true"></property> 
     <property name="ignoreUnresolvablePlaceholders" value="true"/> 
     <property name="locations"> 
      <list> 
       <value>classpath:applicationHost.properties</value> 
      </list> 
     </property> 
</bean>