spring - How to set List of java.util.Locale in Spring XML? -
how can configure list of java.util.locale in spring xml?
this tried (which didn't work..):-
<bean class="x.y.z.commandbean" scope="prototype"> <property name="locales"> <list value-type="java.util.locale"> <value>locale.us</value> <value>locale.fr</value> </list> </property> </bean>
exception :-
org.springframework.beans.typemismatchexception: failed convert value of type 'java.lang.string' required type 'java.util.locale';
also, there way can move locale comma separated values in .properties file?
specifying values
<value>java.util.locale.us</value> <value>java.util.locale.fr</value>
should trick. getting them properties value seems bit more work.
you specify them like
my.app.locales=en_us,de_de
configuring
<bean class="org.springframework.beans.factory.config.propertyplaceholderconfigurer"> <property name="location"> <value>file:./config.properties</value> </property> </bean> <bean class="x.y.z.commandbean" scope="prototype"> <property name="locales"> <bean class="org.springframework.util.stringutils" factory-method="tokenizetostringarray"> <constructor-arg type="java.lang.string" value="${my.app.locales}"/> <constructor-arg type="java.lang.string" value=","/> </bean> </property> </bean>
and need
import org.apache.commons.lang3.localeutils; public void setlocales(string[] localestrings) { list<locale> locales = new arraylist<locale>(localestrings.length); (string localename: arrays.aslist(localestrings)) { locales.add(localeutils.tolocale(localename)); } this.locales = locales; }
this bit gludgy though. alternative, define wrapper class conversion above, , wire bean. hook class bean.
Comments
Post a Comment