spring中自定义属性编辑器注入org.springframework.beans.factory.config.CustomEditorConfigurer时报错问题

在Spring中自定义属性编辑器时遇到错误,报错与旧版到新版`CustomEditorConfigurer`的`customEditors`属性变化有关。原本注入的方式不再适用,因为新版本的`customEditors`要求为`Map<Class<?>, Class<? extends PropertyEditor>>`类型。解决方案是更新配置方式以适配新版本的API。" 108756529,9340802,IntelliJ IDEA快捷键设置指南,"['intellij idea', '开发工具', 'IDE']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

报错信息如下:

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的配置方式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值