spring 中的多个属性。 属性 在 PropertyPlaceholderConfigurer class

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 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.FooServiceServiceInstanceLock.<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"

尝试替换

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

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

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