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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24158385/
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-08-14 10:32:50  来源:igfitidea点击:

Importing Spring properties files from jar file on classpath

javaspring

提问by achingfingers

I want to import all property files, ending on .propertiesthat are contained in the src/main/resourcelocations of ALLjar-dependencies my project has.

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

I wrote a JUnit test, where my context.xml is located in the src/test/resources folder. I specified the property-placeholder using wildcards, but it doesn't work.

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

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

May be I am to stupid, but I could not find a solution to my problem on the net. Does anyone here know what is the correct syntax?

可能是我太笨了,但我在网上找不到解决我的问题的方法。这里有人知道正确的语法是什么吗?

EDIT:

编辑:

The root project, has maven dependencies, that are resolved from my workspace:

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

enter image description here

enter image description here

And i want to import the module.properties files of the dependent projects:

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

enter image description here

enter image description here

采纳答案by achingfingers

From the Spring documentation:

从 Spring文档

The " classpath*:" prefix can also be combined with a PathMatcher pattern in the rest of the location path, for example " classpath*:META-INF/*-beans.xml". [...]

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

But there is a restriction:

但是有一个限制:

Please note that " classpath*:" when combined with Ant-style patterns will only work reliably with at least one root directory before the pattern starts, unless the actual target files reside in the file system. This means that a pattern like " classpath*:*.xml" will not retrieve files from the root of jar files but rather only from the root of expanded directories. [...]

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

So if I place my module's property files in src/main/resources/META-INF, I can load of them as follows:

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

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

回答by user3720852

if the properties that you are using is less than 4. You can use this:

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

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

Else use this

否则用这个

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

回答by ZaoTaoBao

You can also do it sth like this:

你也可以这样做:

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

more complex example:

更复杂的例子:

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