【发布时间】:2015-06-06 09:39:09
【问题描述】:
我正在尝试创建 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”?
【问题讨论】:
-
我现在也有同样的问题。我认为不可能在第二个占位符中使用第一个占位符的属性,而只能使用环境属性。最后找到解决办法了吗?
-
@superbly,你是绝对正确的。变量在 PropertyPlaceholderConfigurer 初始化后变得可用。我刚刚创建了新 bean
将其添加到 propertyconfigurer bean#{envProperties['my.init.dir']}/main.properties -
也在考虑这个问题。最后我使用了以下内容:stackoverflow.com/a/31205614/606496
标签: java spring spring-mvc properties