春天忽略资源不发现不工作

Posted

技术标签:

【中文标题】春天忽略资源不发现不工作【英文标题】:spring ignoreResourceNotFound is not working 【发布时间】:2017-12-07 10:38:31 【问题描述】:

我已经配置了如下属性文件

<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=true,因此如果找不到属性文件,我预计不会出现任何错误。

但是如果缺少属性文件,我的 spring 上下文初始化会失败。我在这里做错了吗?感谢您的帮助。

错误堆栈跟踪:

> Error creating bean with name 'instanceConfigurati
>     on': Unsatisfied dependency expressed through field 'appsDeployedProperties': Ex
>     pression parsing failed; nested exception is org.springframework.expression.spel
>     .SpelEvaluationException: EL1008E:(pos 0): Property or field 'appsDeployedProper
>     ties' cannot be found on object of type 'org.springframework.beans.factory.confi
>     g.BeanExpressionContext' - maybe not public?; nested exception is org.springfram
>     ework.beans.factory.BeanExpressionException: Expression parsing failed; nested e
>     xception is org.springframework.expression.spel.SpelEvaluationException: EL1008E
>     :(pos 0): Property or field 'appsDeployedProperties' cannot be found on object o
>     f type 'org.springframework.beans.factory.config.BeanExpressionContext' -
> maybe
>     not public?; nested exception is org.springframework.beans.factory.UnsatisfiedDe
>     pendencyException: Error creating bean with name 'instanceConfiguration': Unsati
>     sfied dependency expressed through field 'appsDeployedProperties': Expression pa
>     rsing failed; nested exception is org.springframework.expression.spel.SpelEvalua
>     tionException: EL1008E:(pos 0): Property or field 'appsDeployedProperties' canno
>     t be found on object of type 'org.springframework.beans.factory.config.BeanExpre
>     ssionContext' - maybe not public?; nested exception is org.springframework.beans
>     .factory.BeanExpressionException: Expression parsing failed; nested exception is
>      org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): P
>     roperty or field 'appsDeployedProperties' cannot be found on object of type 'org
>     .springframework.beans.factory.config.BeanExpressionContext' - maybe not public?at
> 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) ~[spring-beans-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]

【问题讨论】:

请添加堆栈跟踪 添加了错误堆栈跟踪 能否请您重新格式化堆栈跟踪,这种形式不可读 【参考方案1】:

您必须将来自applicationHost.properties 的键绑定为String 而不是Properties。比如你在applicationHost.properties中有keyappsDeployedProperties,如下:

applicationHost.properties

appsDeployedProperties=hello

在 Spring Bean 中,你必须使用 as String,例如:

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

P.S:你可以添加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>

【讨论】:

以上是关于春天忽略资源不发现不工作的主要内容,如果未能解决你的问题,请参考以下文章

如何忽略/隐藏/停用我的政策不合规状态并获得 100% 合规?

如何从资源文件夹中获取文件。春天框架

eclipse svn同步资源库时忽略某些不需要提交文件类型和文件夹

避免“资源与文件系统不同步”

VS 团队资源管理 强制解锁锁定文件

资源中的 MFC 修改被忽略