2017-07-04 169 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>