报错信息如下:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'PropertyEditorConfigurer1' defined in class path resource [spring/application-ren.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 'renchaofeng_propertyEditor.TestPropertyEditor' to required type 'java.lang.Class' for property 'customEditors[renchaofeng_propertyEditor.Education]': PropertyEditor [org.springframework.beans.propertyeditors.ClassEditor] returned inappropriate value of type 'renchaofeng_propertyEditor.TestPropertyEditor'
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:168)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:687)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:525)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at renchaofeng.TestSpring.main(TestSpring.java:11)
Caused by: 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 'renchaofeng_propertyEditor.TestPropertyEditor' to required type 'java.lang.Class' for property 'customEditors[renchaofeng_propertyEditor.Education]': PropertyEditor [org.springframework.beans.propertyeditors.ClassEditor] returned inappropriate value of type 'renchaofeng_propertyEditor.TestPropertyEditor'
at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:608)
at org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:615)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:216)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1583)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1542)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1284)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
... 11 more
Caused by: java.lang.IllegalArgumentException: Cannot convert value of type 'renchaofeng_propertyEditor.TestPropertyEditor' to required type 'java.lang.Class' for property 'customEditors[renchaofeng_propertyEditor.Education]': PropertyEditor [org.springframework.beans.propertyeditors.ClassEditor] returned inappropriate value of type 'renchaofeng_propertyEditor.TestPropertyEditor'
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:303)
at org.springframework.beans.TypeConverterDelegate.convertToTypedMap(TypeConverterDelegate.java:661)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:227)
at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:588)
... 17 more
有时我们需要定义属性编辑器,便于将string类型转成我们需要的类型,代码如下:
package renchaofeng_propertyEditor;
public class Person {
private int id;
private String name;
private Education education;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Education getEducation() {
return education;
}
public void setEducation(Education education) {
this.education = education;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", education=" + education + "]";
}
}
package renchaofeng_propertyEditor;
public class Education {
private String country;
private String directory;
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getDirectory() {
return directory;
}
public void setDirectory(String directory) {
this.directory = directory;
}
}
package renchaofeng_propertyEditor;
import java.beans.PropertyEditorSupport;
import org.apache.commons.lang3.StringUtils;
public class TestPropertyEditor extends PropertyEditorSupport{
@Override
public void setAsText(String text) throws IllegalArgumentException {
if(text == null || StringUtils.isBlank(text)) {
throw new IllegalArgumentException("参数text不能为空");
}else {
String[] StrAddressArr = StringUtils.split(text, ",");
Education add = new Education();
add.setCountry(StrAddressArr[0]);
add.setDirectory(StrAddressArr[1]);
setValue(add);
}
}
}
然后在spring的配置文件里注入Person类和属性编辑器TestPropertyEditor,如下:
<bean id="person" class="renchaofeng_propertyEditor.Person">
<property name="id" value="1003" />
<property name="name" value="东北大亨" />
<property name="education" value="中国,北京海淀区清华大学" />
</bean>
<!--下面是老版本配置方式,会报上面的错误 -->
<bean id="PropertyEditorConfigurer1"
class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="renchaofeng_propertyEditor.Education">
<bean class="renchaofeng_propertyEditor.TestPropertyEditor" />
</entry>
</map>
</property>
</bean>
<!--下面是新版本配置方式,改成这种方式就会成功 -->
<bean id="PropertyEditorConfigurer1"
class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="renchaofeng_propertyEditor.Education"
value="renchaofeng_propertyEditor.TestPropertyEditor"/>
</map>
</property>
</bean>
原因在于CustomEditorConfigurer类的属性customEditors进行更改,老版本的是定义是private Map customEditors; 而新版本定义如下: private Map<Class<?>, Class<? extends PropertyEditor>> customEditors; 所以要更改一下org.springframework.beans.factory.config.CustomEditorConfigurer的配置方式。