spring源码解析之属性编辑器propertyEditor
造成此异常的原因
bean
配置文件
调用代码
特别说明:
异常解决
注册springt自带的属性编辑器 CustomDateEditor
控制台输出
属性编辑器是何时并如何被注册到spring容器中的?
查看AbstractApplicationContext 的 refresh 方法
属性编辑器是如何被调用的?
分析报错信息
异常信息
Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'date': no matching editors or conversion strategy found
十月 23, 2018 1:40:38 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
資訊: Loading XML bean definitions from class path resource [ApplicationContext.xml]
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'useFunctionService' defined in class path resource [ApplicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'date'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'date': no matching editors or conversion strategy found
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at com.wisely.highlight_spring4.ch1.di.BeanFactoryTest.main(BeanFactoryTest.java:15)
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'date'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'date': no matching editors or conversion strategy found
    at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:476)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:512)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:506)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1523)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1482)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1222)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    ... 6 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'date': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:287)
    at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:461)
    ... 12 more
造成此异常的原因
当bean中属性被声明非String类型时需要做类型转换
bean
import java.util.Date;
import org.springframework.stereotype.Service;
@Service
public class UseFunctionService {
    private Date date;
    public Date getDate()
    {
        return date;
    }
    public void setDate(Date date)
    {
        this.date = date;
    }
}配置文件
<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
    <bean id="useFunctionService" class="com.wisely.highlight_spring4.ch1.di.UseFunctionService">
        <property name="date" value="2018-10-23"></property>
    </bean>
    <bean id="functionService" class="com.wisely.highlight_spring4.ch1.di.FunctionService">
    </bean>
</beans>调用代码
import java.util.Date;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
@SuppressWarnings("deprecation")
public class BeanFactoryTest
{
    public static void main(String[] args)
    {
//        BeanFactory bf = new XmlBeanFactory(new ClassPathResource("ApplicationContext.xml"));
        ApplicationContext bf = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        UseFunctionService useFunc = (UseFunctionService) bf.getBean("useFunctionService");
        Date date = useFunc.getDate();
        System.out.println(date);
    }
}特别说明:
这里必须要用 ApplicationContext 而不是 BeanFactory ,因为是属性编辑器是ApplicationContext 对 BeanFactory 的扩展功能,BeanFactory 实际上是不具备这个功能的,调用的时候会报错.
异常解决
注册springt自带的属性编辑器 CustomDateEditor
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.propertyeditors.CustomDateEditor;
public class DatePropertyEditorRegistrar implements PropertyEditorRegistrar
{
    @Override
    public void registerCustomEditors(PropertyEditorRegistry registry)
    {
        registry.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }
}<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
    <bean id="useFunctionService" class="com.wisely.highlight_spring4.ch1.di.UseFunctionService">
        <property name="date" value="2018-10-23"/>
    </bean>
    <bean id="functionService" class="com.wisely.highlight_spring4.ch1.di.FunctionService">
    </bean>
    <!-- 注册springt自带的属性编辑器 CustomDateEditor-->
    <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
        <property name="propertyEditorRegistrars">
            <list>
                <bean class="com.wisely.highlight_spring4.ch1.di.DatePropertyEditorRegistrar"></bean>
            </list>
        </property>
    </bean>
</beans>
控制台输出

属性编辑器是何时并如何被注册到spring容器中的?
查看AbstractApplicationContext 的 refresh 方法


- 上文中我们在注册属性编辑时用的是 ==PropertyEditorRegistrar== 的 ==registerCustomEditor== 方法,与这里的beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));究竟是怎么联系在一起的呢? 进入ResourceEditorRegistrar内部查看一下.


属性编辑器是如何被调用的?
分析报错信息
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)//创建bean
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1222) //属性注入,此时bean已被实例化但是没初始化
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1523)//类型转换
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:476)//转换
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:512)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:506)spring源码解析之属性编辑器propertyEditor的更多相关文章
- sprin源码解析之属性编辑器propertyEditor
		目录 异常信息 造成此异常的原因 bean 配置文件 调用代码 特别说明: 异常解决 注册springt自带的属性编辑器 CustomDateEditor 控制台输出 属性编辑器是何时并如何被注册到s ... 
- Spring源码解析 - AbstractBeanFactory 实现接口与父类分析
		我们先来看类图吧: 除了BeanFactory这一支的接口,AbstractBeanFactory主要实现了AliasRegistry和SingletonBeanRegistry接口. 这边主要提供了 ... 
- spring 源码解析
		1. [文件] spring源码.txt ~ 15B 下载(167) ? 1 springн┤┬вио╬Ш: 2. [文件] spring源码分析之AOP.txt ~ 15KB 下载( ... 
- Spring源码解析——循环依赖的解决方案
		一.前言 承接<Spring源码解析--创建bean>.<Spring源码解析--创建bean的实例>,我们今天接着聊聊,循环依赖的解决方案,即创建bean的ObjectFac ... 
- Spring源码解析系列汇总
		相信我,你会收藏这篇文章的 本篇文章是这段时间撸出来的Spring源码解析系列文章的汇总,总共包含以下专题.喜欢的同学可以收藏起来以备不时之需 SpringIOC源码解析(上) 本篇文章搭建了IOC源 ... 
- Spring源码解析之BeanFactoryPostProcessor(三)
		在上一章中笔者介绍了refresh()的<1>处是如何获取beanFactory对象,下面我们要来学习refresh()方法的<2>处是如何调用invokeBeanFactor ... 
- Spring源码解析之ConfigurationClassPostProcessor(二)
		上一个章节,笔者向大家介绍了spring是如何来过滤配置类的,下面我们来看看在过滤出配置类后,spring是如何来解析配置类的.首先过滤出来的配置类会存放在configCandidates列表, 在代 ... 
- Spring源码解析之八finishBeanFactoryInitialization方法即初始化单例bean
		Spring源码解析之八finishBeanFactoryInitialization方法即初始化单例bean 七千字长文深刻解读,Spirng中是如何初始化单例bean的,和面试中最常问的Sprin ... 
- Spring源码解析-ioc容器的设计
		Spring源码解析-ioc容器的设计 1 IoC容器系列的设计:BeanFactory和ApplicatioContext 在Spring容器中,主要分为两个主要的容器系列,一个是实现BeanFac ... 
随机推荐
- 提升Idea启动速度与Tomcat日志乱码问题
			提升Idea启动速度与Tomcat日志乱码问题 前言 由于重装了一次Idea,所以有些设置时间就忘了,在此做个记录,以便以后忘记后可以来翻阅 Idea启动速度 一.将Idea所在的 安装文件夹 在wi ... 
- 攻防世界 reverse Mysterious
			Mysterious BUUCTF-2019 int __stdcall sub_401090(HWND hWnd, int a2, int a3, int a4) { char v5; // [e ... 
- django常用模板语言
			一.变量 django模板接收到从后端传来的参数,放入模板中对应的变量中#django代码 def info(request): .... return render(request,'info.ht ... 
- 尝试做一个.NET简单、高效、避免OOM的Excel工具
			Github : https://github.com/shps951023/MiniExcel 简介 我尝试做一个.NET简单.高效.避免OOM的Excel工具 目前主流框架大多将资料全载入到记忆体 ... 
- 前端er必须知道的Git地址及常用工具地址
			商城篇(找工作必练) 开源商城 推荐指数:5星,掌握了它,可以说,今后工作中的各种需求都不是问题,工作1~2年的也可以学习其中的思路(建议收藏). 这是一个集小程序/公众号/app为一体的商城系统,包 ... 
- 【算法学习笔记】组合数与 Lucas 定理
			卢卡斯定理是一个与组合数有关的数论定理,在算法竞赛中用于求组合数对某质数的模. 第一部分是博主的个人理解,第二部分为 Pecco 学长的介绍 第一部分 一般情况下,我们计算大组合数取模问题是用递推公式 ... 
- windows上phpstudy配置memcache
			原文 http://blog.csdn.net/ltx06/article/details/78588448 总的来说,分两步:同时安装memcached软件服务和安装php_memcache ... 
- 如何优雅地学习计算机2<-->Helloworld
			0.导入  在进行粗略的学习计算机底层知识和变量后,我们来开始编写年轻人的第一个程序--Helloworld.  我们需要用到的工具有:1.Dev-C++(也可以使用其他软件)2.脑子(最重要) ... 
- IDEA 配置文件位置
			1 IDEA 2020.1 以上 1.1 Win 语法: %APPDATA%\JetBrains\<product><version> Win上的APPDATA默认位置如下: ... 
- try - with - resource
			本文详细介绍了自 JDK 7 引入的 try-with-resources 语句的原理和用法,以及介绍了 JDK 9 对 try-with-resources 的改进,使得用户可以更加方便.简洁的使用 ... 
