带有通配符的spring <util:properties />

pih*_*agy 4 spring properties

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

<util:properties id="requestProcessorRepository"
  location="classpath*:*requestProcessors.properties"/>
Run Code Online (Sandbox Code Playgroud)

但它无效

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
Run Code Online (Sandbox Code Playgroud)

没有通配符,它​​就会起作用.

那么从通配文件创建属性对象/映射的任何其他可能性?

axt*_*avt 8

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

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

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

<bean id = "requestProcessorRepository" 
    class = "org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name = "locations" 
        value = "classpath*:somefolder/*requestProcessors.properties" />
</bean>
Run Code Online (Sandbox Code Playgroud)

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