Java spring中的多个属性。PropertyPlaceHolderConfigure类中的属性
我正在尝试创建spring项目。我的目标是在未来为jar和war构建项目。 我在包含“conf.path.dir”属性的类路径中使用abc.properties文件。War和jar项目使用不同位置的配置。我将在构建时替换abc.properties,并使用它来配置我的PropertyPlaceholderConfigurer beanJava spring中的多个属性。PropertyPlaceHolderConfigure类中的属性,java,spring,spring-mvc,properties,Java,Spring,Spring Mvc,Properties,我正在尝试创建spring项目。我的目标是在未来为jar和war构建项目。 我在包含“conf.path.dir”属性的类路径中使用abc.properties文件。War和jar项目使用不同位置的配置。我将在构建时替换abc.properties,并使用它来配置我的PropertyPlaceholderConfigurer bean <bean id="first" class="org.springframework.beans.factory.config.PropertyPlace
<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>
类路径:abc.properties
类路径:ax/add.properties
${conf.path.dir}/main.properties
很遗憾,无法确定参数,我得到了以下错误:
{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
{2015\04\01 11:11:58}(错误)#com.mys.FooService#服务出现意外错误:
org.springframework.beans.factory.BeanInitializationException:无法加载属性;嵌套异常为java.io.FileNotFoundException:类路径资源[${conf.path.dir}/main.properties]不存在,因此无法打开它
位于org.springframework.beans.factory.config.PropertyResourceConfiger.postProcessBeanFactory(PropertyResourceConfiger.java:87)
位于org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:694)
位于org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:669)
位于org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
位于org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:139)
位于org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:83)
在com.mys.FooService.start(FooService.java:149)
在com.mys.FooService$1ServiceInstanceLock上。(FooService.java:12)
位于com.mys.FooService.main(FooService.java:137)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)中
位于java.lang.reflect.Method.invoke(Method.java:597)
位于com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
原因:java.io.FileNotFoundException:无法打开类路径资源[${conf.path.dir}/main.properties],因为它不存在
位于org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)
位于org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:143)
位于org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:98)
位于org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:175)
位于org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:156)
位于org.springframework.beans.factory.config.PropertyResourceConfiger.postProcessBeanFactory(PropertyResourceConfiger.java:78)
... 还有13个
Spring版本:3.2.4
如何修复该问题并从abc.properties解析“conf.path.dir”?尝试替换
<value>${conf.path.dir}/main.properties</value>
${conf.path.dir}/main.properties
与
文件://{conf.path.dir}/main.properties
这应该可以解决您的问题。谢谢您的回复,但这无助于“org.springframework.expression.spel.SpelEvaluationException:EL1008E:(位置0):在对象上找不到字段或属性“conf…”abc.properties文件:\n app.root.dir=\n conf.path.dir=${app.root.dir}/conftry在web.xml文件中将其定义为上下文参数,此时我也遇到同样的问题。我认为不可能在第二个占位符中使用第一个占位符的属性,而只能使用环境属性。你最后找到解决办法了吗?@很好,你完全正确。初始化PropertyPlaceHolderConfigure后,变量变为可用。在将新bean添加到propertyconfigurer bean#{envProperties['my.init.dir']}/main.properties之后,我刚刚创建了它。最后,我使用了以下方法:
<value>file:///#{conf.path.dir}/main.properties</value>