CustomDateEditor在Spring 4.1.6中不起作用

时间:2015-05-11 17:25:38

标签: spring

我正在使用Date dob;我在春季4.1.6使用maven的pojo类中的字段     以下是与我的应用程序相关联的文件。

//这是我的pojo课程。

package com.aamir;

import java.util.Date;

public class Student {

private String firstName;
private Date dob;

public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}
public Date getDob() {
    return dob;
}
public void setDob(Date dob) {
    this.dob = dob;
}


}

//这是我的客户端类

package com.aamir;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ClientStud1 {

public static void main(String[] args) {

    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    Student student = (Student) context.getBean("s1");

    System.out.println(student.getFirstName());
    System.out.println(student.getDob()); 

}

} //最后这是我的beans.xml文件

  <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="s1" class="com.aamir.Student">
       <property name="firstName" value="aamir"/>
       <property name="dob" value="12-12-1996"/>
   </bean>

    <bean id="dateEditor" 
               class="org.springframework.beans.propertyeditors.CustomDateEditor">
            <constructor-arg>
                <bean class="java.text.SimpleDateFormat">
                    <constructor-arg value="dd-MM-yyyy"/>
                </bean>
            </constructor-arg>
            <constructor-arg value="true"/>
    </bean>

    <bean  class="org.springframework.beans.factory.config.CustomEditorConfigurer">
       <property name="customEditors">
         <map>
           <entry key="java.util.Date" value-ref="dateEditor"/>
         </map>
       </property>
   </bean>


</beans>

每次我尝试运行客户端时都会收到此异常

org.springframework.beans.factory.BeanCreationException: Error creating bean     with name 'org.springframework.beans.factory.config.CustomEditorConfigurer#0' defined in class path resource [beans.xml]: 
Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.util.LinkedHashMap' to required type 'java.util.Map' for property 'customEditors'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [org.springframework.beans.propertyeditors.CustomDateEditor] to required type [java.lang.Class] for property 'customEditors[java.util.Date]': PropertyEditor [org.springframework.beans.propertyeditors.ClassEditor] returned inappropriate value of type [org.springframework.beans.propertyeditors.CustomDateEditor]

2 个答案:

答案 0 :(得分:1)

请注意,PropertyEditor是有状态的,因此您不应直接注册实例。

  • 通过customEditor属性
  • 注册属性编辑器
  • 通过PropertyEditorRegistrars
  • 中的propertyEditorRegistrars媒体资源添加CustomEditorConfigurer

如果要配置属性编辑器实例,请改用属性编辑器注册器。

    <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
       <property name="propertyEditorRegistrars">
         <list>
           <bean class="org.example.CustomDateEditorRegistrar"/>
         </list>
       </property>
   </bean>

答案 1 :(得分:0)

根据官方文档,注册PropertyEditor的推荐方法是创建实现class接口的propertyEditorRegistrar。每个propertyEditorRegistrar可以在给定的propertyEditors上注册任意数量的registry

下面描述了两个这样的实现:

方法1:创建一个实现PropertyEditorRegistrar的通用类。

此类可用于任何propertyEditor(s),因此您可以将此类保留在utils中。

让我们将其命名为CustomEditorRegistrar,它看起来像这样:

public class CustomEditorRegistrar implements PropertyEditorRegistrar
{
    Class<?>        t;
    PropertyEditor  propertyEditor;

    public CustomEditorRegistrar(Class<?> t, PropertyEditor propertyEditor)
    {
        this.t = t;
        this.propertyEditor = propertyEditor;
    }

    @Override
    public void registerCustomEditors(PropertyEditorRegistry registry)
    {
        registry.registerCustomEditor(t, propertyEditor);
    }

}

。注册CustomDateEditor的bean定义如下:

    <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
        <property name="propertyEditorRegistrars">
            <list>
                <bean class="mky.spring.property.editor.date.CustomEditorRegistrar">
                    <constructor-arg index="0">
                        <value type="java.lang.Class">java.util.Date</value>
                    </constructor-arg>
                    <constructor-arg index="1">
                        <bean class="org.springframework.beans.propertyeditors.CustomDateEditor">
                            <constructor-arg index="0">
                                <bean class="java.text.SimpleDateFormat">
                                    <constructor-arg value="dd-MM-yyyy" />
                                </bean>
                            </constructor-arg>
                            <constructor-arg index="1" value="true" />
                        </bean>
                    </constructor-arg>
                </bean>
            </list>
        </property>
    </bean>
    <bean
        name="Customer"
        class="mky.spring.property.editor.date.Customer"
        p:name="Customer 1"
        p:address="CounterHills LA"
        p:deliveryDate="12-11-2016"
        p:issueDate="12-10-2016" />

Sys-out客户:

public class TestGenericClient
{
    ApplicationContext  ctx;

    public TestGenericClient()
    {
        ctx = new ClassPathXmlApplicationContext("genericPropertyEditorRegistrar-beans.xml");
    }

    public static void main(String[] args)
    {
        System.out.println(new TestGenericClient().ctx.getBean("Customer"));
    }
}

输出是这样的:

Name=Customer 1
 Address=CounterHills LA
 Issue Date=Wed Oct 12 00:00:00 GST 2016
 deliveryDate=Sat Nov 12 00:00:00 GST 2016

方法2:创建一个特定的propertyEditorRegistrar,例如,for Date

public class CustomDateEditorRegistrar implements PropertyEditorRegistrar
{

    @Override
    public void registerCustomEditors(PropertyEditorRegistry registry)
    {
        registry.registerCustomEditor(java.util.Date.class, new CustomDateEditor(new SimpleDateFormat("dd-MM-yyyyy"),true));
    }

}

此特定CustomDateEditor的bean声明是:

<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
        <property name="propertyEditorRegistrars">
            <list>
                <bean class="mky.spring.property.editor.date.CustomDateEditorRegistrar" />

            </list>
        </property>
    </bean>

Sys-out cleint:

public TestDateClient()
    {
        ctx = new ClassPathXmlApplicationContext("datePropertyEditorRegistrar-beans.xml");
    }

    public static void main(String[] args)
    {
        System.out.println(new TestDateClient().ctx.getBean("Customer"));
    }

同样的输出:

Name=Customer 1
 Address=CounterHills LA
 Issue Date=Wed Oct 12 00:00:00 GST 2016
 deliveryDate=Sat Nov 12 00:00:00 GST 2016