【问题标题】:multiple properties in spring. property in PropertyPlaceholderConfigurer class春天的多个属性。 PropertyPlaceholderConfigurer 类中的属性
【发布时间】: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


【解决方案1】:

尝试替换

<value>${conf.path.dir}/main.properties</value>

<value>file:///#{conf.path.dir}/main.properties</value>

这应该可以解决您的问题。

【讨论】:

  • 感谢您的回复,但没有帮助“org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Field or property 'conf' cannot be found on object.. 。”
  • abc.properties 文件:\n app.root.dir = . \n conf.path.dir = ${app.root.dir}/conf
  • 尝试在您的 web.xml 文件中将其定义为上下文参数
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-28
  • 1970-01-01
  • 2018-11-29
  • 2016-06-01
  • 2018-07-26
  • 2015-09-04
  • 2010-12-08
相关资源
最近更新 更多