Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring util:properties - can you change the encoding to UTF-8?

I'm working on converting some properties files from iso-8859-1 to utf-8. I implemented a resource.control for most resource files so that it would read as utf-8 encoding.

I came across a properties file that was defined in spring using the util:properties tag.

I was wondering if there is a way to specify that the default encoding be utf-8? I did look at the possibility defining this as a bean with the ReloadableResourceBundleMessageSource, however that would require a big refactoring process, since the logic in the bean expects this to be as a map.

defined using spring 3.0.5

<util:properties id="fooProperties" location="file:${AXE_APPCONFIG}/foo.properties"/>

I know that by definition java properties files are iso-8859-1 encoded, however I thought that spring might have come up with a way to change the encoding of it (like the Resource.Control)

like image 312
phanneman Avatar asked Dec 28 '11 23:12

phanneman


1 Answers

I have recently solved the same problem by using PropertiesFactoryBean. It is a subclass of PropertiesLoaderSupport, which has a public setter method for you to specify encoding of your property file.

<beans:bean id="nameOfYourPropsVar" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <beans:property name="location" value="classpath:${yourPropFileName}"/>
    <beans:property name="fileEncoding" value="UTF-8"/>
</beans:bean>

Please note that according to the api doc, it only applies to classic properties file, not to XML files.

like image 61
Wei Li Avatar answered Sep 30 '22 15:09

Wei Li