在Spring应用程序中加载内部(Classpath)和外部属性文件

时间:2014-04-15 12:33:50

标签: java spring spring-mvc

我需要在spring应用程序中加载外部和内部属性文件。一旦我将外部文件声明如下

<context:property-placeholder location="file:${JBOSS_HOME}/123.properties" />

我可以访问此外部文件中定义的属性。但是我的类路径中与属性文件相关的所有属性都无法解析。

我的应用上下文

** <!--Refer External File --> **
<context:property-placeholder location="file:${JBOSS_HOME}/123.properties" />

 <!--Refer Internal File -->

<bean id="helloWorldBean"
    class="com.javacodegeeks.snippets.enterprise.services.HelloWorld">
    <property name="internalProperty1" value="${internalProperty1}" /> 

    <property name="**externalProperty**" value="${**externalProperty**}" />
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>constants.properties</value>
    </property>
</bean>

我获取外部属性文件的属性值,但不获取内部属性文件的值。

主题“主要”

中的例外情况
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'helloWorldBean' defined in class path resource [applicationContext.xml]: Could not resolve placeholder 'internalProperty1' in string value "${internalProperty1}"
    at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:209)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.processProperties(PropertySourcesPlaceholderConfigurer.java:174)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:151)
    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>(ClassPathXmlApplsamicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.javacodegeeks.snippets.enterprise.App.main(App.java:13)

我不能一起加载外部(非类路径)和内部(类路径)属性文件吗?

1 个答案:

答案 0 :(得分:1)

你需要的是这样的东西:

    <!--Order matters, properties in the second file will override the first -->
<context:property-placeholder location="file:${JBOSS_HOME}/123.properties,classpath:configuration.properties"
ignore-unresolvable="false" ignore-resource-not-found="true" />
相关问题