Kasun
Kasun

Reputation: 561

Load internal (Classpath) and external property file in a Spring Application

I need to load both external and internal property files in my spring application. Once I declare the external file as below

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

I can access the properties defined in this external file. But all properties related to the property file in my class path Could not resolved.

My Application Context

** <!--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>

I am getting property value of the external property file but not the value of the internal property file.

Exception in thread "main"

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)

Cannot I load external(non-class path) and internal (Class path) property file together ?

Upvotes: 2

Views: 3334

Answers (1)

geoand
geoand

Reputation: 64069

What you need is something like this:

    <!--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" />

Upvotes: 1

Related Questions