Spring: Autowiring of a bean of type java.util.Locale does not seem to work
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:
- Configuration is purely XML based.
Bean definition开发者_高级运维s 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());
}
}
- If I add a field to the SimpleDateTimeBean3 class, which type is one of my own classes and such bean exists, everything works perfectly.
- No errors are printed, both beans (spanishLocale, dateTimeBeanSetter) are created and accessible from ApplicationContext, but calling getDateTime() on the 'dateTimeBeanSetter' bean gives a NPE.
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.
精彩评论