我想从多个位置加载键值对。我的第一个猜测是:
<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
没有通配符,它就会起作用。
那么从通配文件创建属性对象/地图的任何其他可能性呢?
答案 0 :(得分:8)
首先,您的资源路径classpath*:*requestProcessors.properties
不可靠(来自Spring Reference):
请注意“classpath *:”时 结合Ant风格的图案会 只能与至少一个人可靠地工作 模式之前的根目录 启动,除非是实际的目标文件 驻留在文件系统中。这意味着 像“classpath *:*。xml”这样的模式 不会从根检索文件 jar文件,而只是来自 扩展目录的根目录。
如果您解决了这个问题,可以按如下方式加载属性:
<bean id = "requestProcessorRepository"
class = "org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name = "locations"
value = "classpath*:somefolder/*requestProcessors.properties" />
</bean>
(代码的原始版本更复杂,但没有必要,因为Spring会自动将带有通配符的路径转换为Resource[]
。