繁体   English   中英

从类路径上的jar文件导入Spring属性文件

[英]Importing Spring properties files from jar file on classpath

我想导入所有属性文件,以.properties结尾,这些文件包含在我的项目所有的 jar依赖项的src/main/resource位置中。

我写了一个JUnit测试,其中我的context.xml位于src / test / resources文件夹中。 我使用通配符指定了property-placeholder,但它不起作用。

<context:property-placeholder location="classpath*:*.properties"/>

可能我是愚蠢的,但我无法在网上找到我的问题的解决方案。 这里有没有人知道什么是正确的语法?

编辑:

根项目,具有maven依赖项,从我的工作区解析:

在此输入图像描述

我想导入依赖项目的module.properties文件:

在此输入图像描述

从Spring 文档

“classpath *:”前缀也可以与位置路径的其余部分中的PathMatcher模式组合,例如“classpath *:META-INF / * - beans.xml”。 [...]

但是有一个限制:

请注意,“类路径*:”与Ant样式模式结合使用时,只能在模式启动前与至少一个根目录可靠地工作,除非实际目标文件驻留在文件系统中。 这意味着像“classpath *:*。xml”这样的模式不会从jar文件的根目录中检索文件,而只能从扩展目录的根目录中检索文件。 [...]

因此,如果我将模块的属性文件放在src / main / resources / META-INF中,我可以按如下方式加载它们:

<context:property-placeholder location="classpath*:/META-INF/*.properties" />

你也可以这样做:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath*:your.properties</value>
                    <value>classpath*:your.properties</value>
                     .....
                </list>
            </property>
            <property name="ignoreUnresolvablePlaceholders" value="true"/>
        </bean>

更复杂的例子:

<bean id="propertyPlaceholderConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreResourceNotFound" value="true" />
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="locations">
        <list>
            <value>classpath*:properties/defaults.properties</value>
            <value>classpath*:properties/${props.env.name}.properties</value>

            <value>classpath*:com/calciuum/config/defaults.properties</value>
            <value>classpath*:com/calciuum/config/${props.env.name}.properties</value>

            <value>classpath*:${props.env.classpath}/defaults.properties</value>
            <value>classpath*:${props.env.classpath}/${props.env.name}.properties</value>

            <value>file:${props.env.ext.properties}</value>
        </list>
    </property>
</bean>

如果您使用的属性小于4.您可以使用:

<context:property-placeholder location="classpath:test1.properties,classpath:test2.properties" />

否则使用它

<context:property-placeholder location="classpath:*.properties" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM