春天的多重属性。 PropertyPlaceholderConfigurer类中的属性

时间:2015-04-01 16:21:52

标签: 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.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”?

1 个答案:

答案 0 :(得分:0)

尝试替换

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

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

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