This is very small thing which can be very annoying while coding in spring framework. Of cource no one can directly understand the problem until he faced that.
At first, I had only one property file from which i am reading some values into spring applicationContext Xml configuration file.So when i needed to add second one for my new feature, i think it is just to add new propertyPlaceholderConfigurer bean with second prop file.
my first prop file 'default-prop.properties' is :
so while adding second one, i have just added second bean for it like:
I have changed the bean definition as,
And voila! it solved my problem...
You can also use
And my problem is resolved.
Give your comments on this problem and solution.
At first, I had only one property file from which i am reading some values into spring applicationContext Xml configuration file.So when i needed to add second one for my new feature, i think it is just to add new propertyPlaceholderConfigurer bean with second prop file.
my first prop file 'default-prop.properties' is :
default-prop.custName=testing customerand the bean definition in the applicationContext xml file for this is:
default-prop.address=building1
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:default-prop.properties</value>
</property>
</bean>
so while adding second one, i have just added second bean for it like:
<bean id="propertyConfigurerNew"and in xml, prop has been read as,
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:second-prop.properties</value>
</property>
</bean>
<property name="customerName" value="${default-prop.custName}" />But when i done this, i got error
"Could not resolve placeholder 'default-prop.custName' "So i have to do some workaround and digg into spring references. And i found that we can define seperate placeHolderPrefix and suffix for each property configurer bean.
I have changed the bean definition as,
<bean id="propertyConfigurer"it means, for first prop bean, we will use ${default-prop.custName} and for second bean we will use #[second-prop.secondName]
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:default-prop.properties</value>
</property>
<property name="placeholderPrefix" value="${" />
<property name="placeholderSuffix" value="}" />
</bean>
<bean id="propertyConfigurerNew"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:second-prop.properties</value>
</property>
<property name="placeholderPrefix" value="#[" />
<property name="placeholderSuffix" value="]" />
</bean>
And voila! it solved my problem...
You can also use
<property name="ignoreUnresolvablePlaceholders" value="true" />for each PropertyPlaceholderConfigurer bean defined. But, i have used the prefix-suffix one solution.
And my problem is resolved.
Give your comments on this problem and solution.
6 Comments:
You can very simply add multiple property files with a single PropertyPlaceholderConfigurer. Instead of 'location' use 'locations'
When two files are being imported into a single file, and each of the two tries to read its own Properties file, this is still a problem. Is there any other solution?
Tell me if i don't understand your question ok?
See here what i have shown is, using multiple poperty files in one xml config file.
I have successfully done this as i shown, declare two files with different prefix and suffix for placeholder.
And its working fine for me. I can access both file properties.
Yet i have not tried the way of first comment,using 'locations' property for 'PropertyPlaceholderConfigurer'. You can try it if it works...
reply if you need something more.
This solution is more appropriate when you want to define propertyplaceholderconfigurer in another xml and include that optionally. For example some beans that are used in production but not used in development could be defined and configured there.
This post helped me out today and now I actually understand that requirement :)
Thanks for sharing!
Excellent post and much appreciated. Saved me a lot of time.
T
I have tried using the 'locations' property for 'PropertyPlaceholderConfigurer' and it works if you want to define multiple property files in one PropertyPlaceHolderConfigurer bean, but it doesnt work if you want to define multiple PropertyPlaceHolderConfigurer beans.
The technique described in this post, however, works wonders if you want different to define a different PropertyPlaceHolderConfigurer beans for each of the context xml files included in your project.
Thank you for this very helpful post!
Post a Comment