xml - Spring:java.util.Locale 类型的 bean 的 Autowirin

我正在为我的开发人员编写一个非常简单的关于 Spring (3.0.x) 的教程,并且遇到了一个奇怪的行为:java.util.Locale 类型的 bean 没有自动连接到其他 bean 中,并且没有错误消息。但是,另一个 bean 仍然可以正常创建,只是该字段为空。

详情:

  • 配置完全基于 XML。
  • 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());
    }   
}
  • 如果我向 SimpleDateTimeBean3 类添加一个字段,该类型是我自己的类之一并且存在这样的 bean,那么一切都会完美无缺。
  • 没有打印错误,两个 bean(spanishLocale、dateTimeBeanSetter)都已创建并可从 ApplicationContext 访问,但是在“dateTimeBeanSetter”bean 上调用 getDateTime() 会产生 NPE。

有什么想法吗?

提前致谢。

最佳答案

两个关键文档片段:

来自引用手册第 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/

相关文章:

maven-2 - Maven : Could not transfer artifact Erro

spring - 无法从 InputStream : Invalid Content-Type:te

python - 在 Matplotlib 中设置颜色图的范围

database-design - ERD - 鱼尾纹符号

sql-server - 从游标源 SQL Server 中删除行

PHP 5.2 无法解释 - 文件停止正常执行

msbuild - 如何转储从 MSBuild 导入的属性表列表

jsf - 如何注入(inject)不同的子类作为 ManagedProperty JSF 2?

python - 如何在 setup.py 中为 cython 设置 sysroot

asp.net - NHibernate:获取所有打开的 session