Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从类路径上的jar文件导入Spring属性文件_Java_Spring - Fatal编程技术网

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

Java 从类路径上的jar文件导入Spring属性文件,java,spring,Java,Spring,我想导入所有属性文件,以.properties结束,这些属性包含在src/main/resource项目的所有jar依赖项的位置中 我编写了一个JUnit测试,其中context.xml位于src/test/resources文件夹中。我使用通配符指定了属性占位符,但它不起作用 <context:property-placeholder location="classpath*:*.properties"/> 也许我太笨了,但我在网上找不到解决问题的办法。这里有人知道正确的语法

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

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

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

也许我太笨了,但我在网上找不到解决问题的办法。这里有人知道正确的语法吗

编辑:

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

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


如果您使用的属性少于4。您可以使用以下选项:

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

否则就用这个

<context:property-placeholder location="classpath:*.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>

classpath*:您的.properties
classpath*:您的.properties
.....
更复杂的例子:

<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>

classpath*:properties/defaults.properties
classpath*:properties/${props.env.name}.properties
classpath*:com/calciuum/config/defaults.properties
classpath*:com/calciuum/config/${props.env.name}.properties
classpath*:${props.env.classpath}/defaults.properties
classpath*:${props.env.classpath}/${props.env.name}.properties
文件:${props.env.ext.properties}
来自弹簧:

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

但有一个限制:

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

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

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


我很怀疑你能在春天做到这一点。location的xsd特别声明“要解析占位符的属性文件的位置,作为Spring资源位置:URL,类路径:伪URL或相对文件路径。可以指定多个位置,用逗号分隔。如果既不指定位置也不指定属性引用,占位符将根据系统属性解析“你试过吗this@DevPa这不会增加对文件系统位置的依赖性问题吗?是否可以将其表示为相对路径?它们位于
src/main/resources
中,而不是
src\main\resources\some dir
?这个jar在您的测试类路径上也可用吗?您是如何将其添加到类路径中的?您应该将其添加为maven依赖项—目前您正在混合maven和eclipse本地依赖项(据我所知,这是等待发生的麻烦)。如果项目在本地工作区中,EclipseMaven集成应该能够获取它。如果这个项目已经有一个maven依赖项,它可能会覆盖本地工作区中的内容,这意味着没有
modules.properties
,因为这似乎是一个新文件。