我正在为我的开发人员编写一个非常简单的关于 Spring (3.0.x) 的教程,并且遇到了一个奇怪的行为:java.util.Locale 类型的 bean 没有自动连接到其他 bean 中,并且没有错误消息。但是,另一个 bean 仍然可以正常创建,只是该字段为空。
详情:
Bean定义如下:
<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"/>
未使用 Autowiring 定制。
类:
包 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());
}
}
有什么想法吗?
提前致谢。
最佳答案
两个关键文档片段:
来自引用手册第 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
来自 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.
所以,按设计工作。
关于xml - Spring:java.util.Locale 类型的 bean 的 Autowiring 似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7484532/