Spring 的 PropertyPlaceHolderConfigurer 不会忽略无法解析的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12770133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:25:06  来源:igfitidea点击:

Spring's PropertyPlaceHolderConfigurer won't ignore unresolvable files

spring

提问by kgautron

I am using spring's PropertyPlaceHolderConfigurer as follows :

我使用 spring 的 PropertyPlaceHolderConfigurer 如下:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="locations">
        <list>
            <value>classpath:default.properties</value>
            <value>file:${user.home}/webextractor.properties</value>
        </list>
    </property>
</bean>

Despite having set the ignoreUnresolvablePlaceholdersproperty to true, I still get a FileNotFoundExceptionon /home/kaykay/webextractor.properties. I know I could just create this file and leave it empty, but I'd like to know what is wrong here.

尽管已经设置的ignoreUnresolvablePlaceholders属性true,我仍然获得FileNotFoundException/home/kaykay/webextractor.properties。我知道我可以创建这个文件并将其留空,但我想知道这里有什么问题。

回答by AxxA Osiris

The ignoreUnresolvablePlaceholdersset to true will ignore placeholders that are not set and not throw an exception. For example if you have the following property in your class @Value("${person.age}")and no corresponding value set in your properties file.

ignoreUnresolvablePlaceholders设置为true,将忽略未设置占位符,而不是抛出异常。例如,如果您的类中有以下属性,@Value("${person.age}")而您的属性文件中没有设置相应的值。

The ignoreResourceNotFoundproperty set to true will have the behavior you expected, that is ignore a resource that isn't found.

ignoreResourceNotFound设置为 true的属性将具有您预期的行为,即忽略未找到的资源。

Hope this helped.

希望这有帮助。

回答by Ashish

I have gone through your problem , I think Osiris is right about the property ignoreUnresolvablePlaceholders. But in case of your , you should to set the property ignoreResourceNotFoundtrue. So that , if the file is not existing then it will ignore that file.

我已经解决了你的问题,我认为 Osiris 对财产的看法是正确的ignoreUnresolvablePlaceholders。但是对于您的 ,您应该将属性设置为ignoreResourceNotFoundtrue。因此,如果该文件不存在,那么它将忽略该文件。

Modified code will be

修改后的代码将是

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
        <list>
            <value>classpath:default.properties</value>
            <value>file:${user.home}/webextractor.properties</value>
        </list>
    </property>
</bean>

try this code and let me know.

试试这个代码,让我知道。