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

I am working on converting some property files from iso-8859-1 to utf-8. I have implemented resource.control for most resource files so that it reads as utf-8 encoding.

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

I was wondering if there is a way to indicate that the default encoding will be utf-8? I looked at the opportunity to define this as a bean with the ReloadableResourceBundleMessageSource resource, however, this will require a great refactoring process, since the logic in the bean expects it to be like a map.

defined using spring 3.0.5

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

I know that by definition, java property files are encoded by iso-8859-1, however, I thought spring might come up with a way to change its encoding (e.g. Resource.Control)

+5
source share
2 answers

I recently solved the same problem using PropertiesFactoryBean . This is a subclass of PropertiesLoaderSupport , which has a public setter method so that you can specify the encoding of your properties 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 applies only to the classic properties file, not to the XML files.

+8
source

, . Spring ( 3.0) , ISO-8859-1.

, .

Properties propsToLoad = new Properties();
InputStream stream = new FileInputStream("filename.properties");
propsToLoad.load(new InputStreamReader(stream,"UTF-8));

Map<String,String> mapYouWant = new HashMap<String,String>((Map) propsToLoad);
-1

All Articles