春天< util:properties/>带通配符 [英] spring <util:properties /> with wildcards

查看:165
本文介绍了春天< util:properties/>带通配符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从多个位置加载键值对.我的第一个猜测是:

I'd like to load key-value pairs from multiple locations. My first guess was:

<util:properties id="requestProcessorRepository"
  location="classpath*:*requestProcessors.properties"/>

但无效

Invocation of init method failed; nested exception is java.io.FileNotFoundException: class path resource [classpath*:*requestProcessors.properties] cannot be opened because it does not exist
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1412)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:546)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:872)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:84)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
    at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:280)
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304)
    ... 24 more

没有通配符,它​​开始工作.

Without wildcards, it starst to work.

从通配符文件创建属性对象/地图还有其他可能性吗?

So any other possibilities to create a properties object / map from wildcarded files?

推荐答案

首先,您的资源路径classpath*:*requestProcessors.properties不可靠(来自

First of all, your resource path classpath*:*requestProcessors.properties is not reliable (from Spring Reference):

当出现以下情况时,请注意"classpath *:" 结合蚂蚁风格的图案会 只能与至少一个可靠地工作 模式之前的根目录 启动,除非实际的目标文件 驻留在文件系统中.这表示 类似于"classpath *:*.xml"的模式 不会从根目录检索文件 jar文件,但仅来自 扩展目录的根.

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.

如果解决此问题,则可以按以下方式加载属性:

If you solve this problem, you can load properties as follows:

<bean id = "requestProcessorRepository" 
    class = "org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name = "locations" 
        value = "classpath*:somefolder/*requestProcessors.properties" />
</bean>

(原始版本的代码更复杂,但是没有必要,因为Spring会将带有通配符的路径自动转换为Resource[].)

(Original version of the code was more complex, but there is no need in it, since Spring converts path with wildcards to Resource[] automatically).

这篇关于春天&lt; util:properties/&gt;带通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆