I am writing a very simple tutorial about Spring (3.0.x) for my fellow developers and have encountered a weird behaviour: bean of type java.util.Locale is not autowired into other bean and there is no error message. But still, the other bean gets created ok, just the field is null.
To the details:
Bean definitions are as follows:
<bean id="spanishLocale" class="java.util.Locale">
<constructor-arg value="es"/>
<constructor-arg value="ES"/>
</bean>
<bean id="dateTimeBeanSetter" class="com.bsl.training.theclock.SimpleDateTimeBean3" autowire="byType"/>
No autowire customisation has been used.
Class:
package com.bsl.training.theclock;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
public class SimpleDateTimeBean3 {
private Locale locale;
public SimpleDateTimeBean3() {
}
public void setLocale(final Locale loc) {
locale = loc;
}
public Locale getLocale() {
return locale;
}
public String getDateTime() {
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale);
return df.format(new Date());
}
}
Any ideas?
Thanks in advance.
Two key documentation fragments:
From reference manual section 3.4.5.1:
You cannot autowire so-called simple properties such as primitives, Strings, and Classes (and arrays of such simple properties). This limitation is by-design
And from org.springframework.beans.BeanUtils#isSimpleProperty() javadoc:
Check if the given type represents a "simple" property: a primitive, a String or other CharSequence, a Number, a Date, a URI, a URL, a Locale, a Class, or a corresponding array.Check if the given type represents a "simple" property: a primitive, a String or other CharSequence, a Number, a Date, a URI, a URL, a Locale, a Class, or a corresponding array.
So, Working As Designed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With