![](/img/trans.png)
[英]Not able to read properties from PropertyPlaceholderConfigurer class spring
[英]multiple properties in spring. property in PropertyPlaceholderConfigurer class
我正在尝试创建spring项目。 我的目标是将来为Jar和War建立项目。 我在包含“ conf.path.dir”属性的类路径中使用abc.properties文件。 War和jar项目使用来自不同位置的配置。 而且我将在构建时替换abc.properties并将其用于配置我的PropertyPlaceholderConfigurer bean。
<bean id="first" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
autowire-candidate="false">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath:abc.properties</value>
</list>
</property>
</bean>
<bean id="second" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" depends-on="first">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath:ax/add.properties</value>
<value>${conf.path.dir}/main.properties</value>
</list>
</property>
</bean>
不幸的是,无法确定参数,并且出现以下错误:
{2015\04\01 11:11:58} (ERROR) #com.mys.FooService# The service got unexpected error:
org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [${conf.path.dir}/main.properties] cannot be opened because it does not exist
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:87)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:694)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:669)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.mys.FooService.start(FooService.java:149)
at com.mys.FooService$1ServiceInstanceLock.<init>(FooService.java:12)
at com.mys.FooService.main(FooService.java:137)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.io.FileNotFoundException: class path resource [${conf.path.dir}/main.properties] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)
at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:143)
at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:98)
at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:175)
at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:156)
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:78)
... 13 more
春季版本:3.2.4如何解决该问题并从abc.properties中解析“ conf.path.dir”?
尝试更换
<value>${conf.path.dir}/main.properties</value>
与
<value>file:///#{conf.path.dir}/main.properties</value>
那应该解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.