我正在嘗試創建彈簧項目。我的目標是在將來爲罐子和戰爭建立項目。 我在包含「conf.path.dir」屬性的類路徑中使用abc.properties文件。戰爭和jar項目使用來自不同位置的配置。我將在構建時替換abc.properties,並使用它來配置我的PropertyPlaceholderConfigurer bean。春天的多個屬性。 PropertyPlaceholderConfigurer類中的屬性
<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
Spring版本:3.2.4 如何解決這個問題並解決abc.properties 「conf.path.dir」 ?
我現在有同樣的問題。我認爲不可能在第二個佔位符中使用第一個佔位符的屬性,但只能使用環境屬性。你最終找到了解決方案嗎? – superbly 2015-07-03 06:55:40
@superbly,你是絕對正確的。變量在PropertyPlaceholderConfigurer初始化後變爲可用。 我剛剛創建了一個新的bean 將它添加到propertyconfigurer bean後 #{envProperties ['my.init.dir']}/main.properties –
user3610089
2015-07-04 09:17:36
也在想這個。最後,我使用了以下內容:http://stackoverflow.com/a/31205614/606496 – superbly 2015-07-06 07:38:50