3

我有一个我的应用程序使用的默认属性文件,但我还需要能够允许一个额外的属性文件,它可以在启动时由 -D 标志指定,并将指向文件系统上的路径(不在我的类路径)。这样做的正确语法是什么?

我试过了,但没有用:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations" 
        value="classpath:/config/config.properties,file:/${additional.configs}" />
    </bean>

因为它抱怨说:

 java.io.FileNotFoundException: class path resource [config/config.properties,file://var/tmp/cfg/qa.properties] cannot be opened because it does not exist

尽管我找到了一些建议这样做的例子,但逗号分隔似乎不起作用。我认为我不能使用它的位置 bean 版本列表,因为附加属性文件是可选的。有什么想法吗?我正在使用弹簧 3.1.1。

谢谢!

更新:使用列表方法有效,但是如何使其成为可选方法仍然很重要。现在我使用ignoreResourceNotFound = true,但这并不理想,因为如果有人错误输入属性,那么它不会失败......

4

2 回答 2

1
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:/config/config.properties</value>
            <value>file:/${additional.configs}</value>
        </list>
    </property>
</bean>

如果它仍然不起作用,您可以尝试指定不带斜线的属性位置:

<value>file:${additional.configs}</value>
于 2013-06-14T20:32:03.693 回答
1

我认为您应该能够通过在您希望成为可选的属性文件上使用通配符来做到这一点。例如

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:/config/config.properties</value>
            <value>file:/${additional.configs}*</value>
        </list>
    </property>
</bean>

显然,您需要将 additional.configs 参数设置为合理的值。您不能在没有文件的系统上将其留空,因为通配符将匹配所有文件!相反,您可以将其设置为不存在的文件的虚拟值。例如

additional.config=/non-existent-file.txt

如果通配符不匹配任何内容,Spring 不会抛出错误,因此这具有使该属性文件可选的效果,而不必求助于 ignoreResourceNotFound=true。

于 2013-12-04T10:55:06.400 回答