Load multiple properties file using <util:properties> in spring 3 - spring

I want to load multiple properties files using <util:properties> tag in a spring 3 application.
I searched on the blogs, but cannot get the correct path to do this.
Hopefully somebody give me the answer to overcome this problem.

Actually <util:properties> is just convenient tag for org.springframework.beans.factory.config.PropertiesFactoryBean. And PropertiesFactoryBean does support multiple locations.
So it is possible to create bean with Properties this way:
<bean id="myProps" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:myprops-common.properties</value>
<value>classpath:myprops-override.properties</value>
<value>classpath:some-more-props-here.properties</value>
</list>
</property>
</bean>

My solution
<context:property-placeholder location="classpath*:*.properties,file:/some/other/path/*.properties" />

util:properties seems to support only 1 properties file (reference). You might want to use the configuration suggested by #peperg.

Related

Loading properties from a file AND system properties using Spring

Searched through some other posts but could not find exactly what I needed, but I would guess this is an easy question..
So I have a property file called myprops.properties
myprops.localProp1=localProp1
myprops.localProp2=localProp2
myprops.systemProp=${systemPropertyName}
Basically, in this property file I want to use the values as is for localProp1 and locapProp2 but for systemProp, I would like to load the system property. Let's assume that the system property is always set.
My spring config xml looks like this...
<bean id="myprops" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<qualifer value="myprops" />
<property name="singleton" value="true"/>
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list><value>classpath:myprops.properties</value></list>
</property>
</bean>
I use the qualifier have this bean autowired and use the qualifier string "myprops" to access it in another class. All the expected values are there except the myprops.systemProp, it still = ${systemPropertyName}.
How would I get this property to be resolved with the actual system property?
I tried the following in my spring config:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties" ref="myprops" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
</bean>
This didn't seem to help..
Any ideas? I'm hoping this is an easy one and I am just misunderstanding a common concept in property configuration.
Note: I had to manually type all the code couldn't copy/paste so please excuse typos.
Thanks.

Accessing properties from PropertyPlaceholderConfigurer with multiple property files

I am new to spring (3.1) and totally stumped by this problem.
I am trying to access a property value "schdestination" using a PropertyPlaceholderConfigurer that is defined in two property files (one overriding the other).
I want to use #Value to set a field in a class and i just can't find a way to do it without using another bean. Here is my spring XML snippet
<bean id="placeholderProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/abc.properties</value>
<value>/WEB-INF/loc.abc.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="order" value="1" />
</bean>
Any clues please?
#Value("${schdestination}")
private String destination;
should work.
The class that contains the #Value needs to be annotated with #Component and you need to have <context:component-scan/> in your applicationContext.xml.
Are you using it in your web app? That was my case. I was loading property files from application context and somehow they were not visible in the web app package - controllers to be precise. I had to re-declare them in servlet-context.xml, then they are visible and work just fine. I am really hoping somebody might shed some light what could be going on, or whether it is truly an issue to be fixed in Spring.

Dynamic spring properties

Hi guys is there any good examples of changing spring properties files content dynamically? I would really appreciate it if you could give me some example or link.
Thanks alot
I think you could use ReloadableResourceBundleMessageSource .It uses java.util.Properties instances as its internal data structure for messages.
Also , as the name suggests , this class supports reloading of properties files through the cacheSeconds setting, and also through programmatically clearing the properties cache. Note that since application servers do typically cache all files loaded from the classpath, you have to put properties files outside of your classpath (WEB-INF/classes) or it'll be cached and won't work.
References / examples / links
http://techdive.in/spring/spring-internationalization-i18n
http://www.jroller.com/raible/entry/spring_mvc_s_reloadableresourcebundlemessagesource
Actually, spring support ${variable} in configration file like below
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>file:${XXX}/XXX.properties</value>
<value>file:${XXX}/YYY.properties</value>
</list>
</property>
</bean>

Spring multiple error messages

I suppose that everybody that uses spring, uses form binding and validation. And you all defined the messages to display on validation errors. I did it with this in my config:
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource"
p:basename="messages" />
what will happen basically is that it will read messages.properties in my root folder of the project.
But I'd need to put messages in two separate files. Because one part of the app has to be standalone. I tried adding this just after the one above:
<bean id="messageSourceAssistenza"
class="org.springframework.context.support.ResourceBundleMessageSource"
p:basename = "com.mypackage.other.assistenzamessages.properties"
/>
but it can't resolve those messages at all. How to solve this?
You should be able to use ResourceBundleMessageSource.setBasenames that accepts array of base names:
Set an array of basenames, each
following ResourceBundle conventions:
essentially, a fully-qualified
classpath location. If it doesn't
contain a package qualifier (such as
org.mypackage), it will be resolved
from the classpath root.
The associated resource bundles will
be checked sequentially when resolving
a message code. Note that message
definitions in a previous resource
bundle will override ones in a later
bundle, due to the sequential lookup.
Sample configuration as follows:
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>messages_1</value>
<value>messages_2</value>
...
<value>messages_n</value>
</list>
</property>
</bean>

Dynamic Spring Message Source

I'm about to extend org.springframework.context.support.AbstractMessageSource that will allow me to dynamically add and edit messages in Spring. I'm planning on storing these values in a database. Is there something out there that does this already? Is there a different approach I should think of?
Here are the requirements:
I have to be able to add messages
I have to be able to edit messages
These adds and edits should take place immediately
Sure.
Develop custom MessageSource and set it as parent to existing (for example based on property files).
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>message/messages</value>
</list>
</property>
<property name="parentMessageSource">
<bean class=com.example.DatabaseMessageSource"/>
</property>
</bean>

Resources