栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【Spring源码】6. emmm...想给Spring添个自定义属性编辑器

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Spring源码】6. emmm...想给Spring添个自定义属性编辑器

我自己个人学习源码的最初目标是为了可以更自由地在源码的基础上进行扩展(正在努力达到),进一步则是希望学习其设计思想,并运用到自己的项目中(目前...呃)。所以在学习的过程中,不单单要捋流程,还要通过拓展的方式捋流程。

看了前几篇的同学应该了解到spring从配置文件中获取到的(无论什么类型)数据最初其实都是字符串的形式,于是有了下面的问题:

  • 这么这些字符串是如何变成我们所希望的类型的呢(如File、InputStream等)?

  • 如果我们想将自己定义的一些特殊的字符串在编译期间就可以转化为指定的类型该如何做呢?

以上内容,都是想写这篇文章的时候现编的(˶‾᷄ ⁻̫ ‾᷅˵)嘿嘿嘿,真正的原因是……

01. 问题的开始(我遇到了一个报错……( ゚д゚))

我有一个,哦不~是两个这样的实体类:

实体类中的一个属性Address是我们的自定义的一个类,我们想在配置文件中进行属性赋值,如下代码:


   
   
   

我希望的是spring在加载AqinEntity这个bean时,会将"浙江省_杭州市"这个字符串中的"浙江省"和"杭州市"分别赋值给Address中的province和city,但如果直接执行,则会报错( ̄◇ ̄;)详情如下:

Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'com.aqin.custom.propertyeditor.Address' for property 'address'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.aqin.custom.propertyeditor.Address' for property 'address': no matching editors or conversion strategy found

分析报错原因,可以看出,报错是因为spring中并不支持类型java.lang.String转换到com.aqin.custom.propertyeditor.Address,那么问题来了,如何使spring支持这种自定义类型转换呢?

于是……(。ì _ í。)有了这篇文章!

02. 正经的开始

本文则是通过添加自定义属性编辑器来掰饬掰饬spring中配置文件里的字符串是如何转化成一些特定的数据类型的。

本文中的扩展想要达到的结果:

解决文章开头的报错问题,将上图中配置文件中配置的字符串形式的address:"浙江省_杭州市",转换成下图中的Address对象:

​说肤浅点儿就是看完本文你将会收获将带有下划线的“浙江省_杭州市”字符串通过我们自定义的属性编辑器直接转换为实体类Address{province,city}的技能W(`0`)W~~~

1. 准备工作 1. 新建实体类

Address.java

package com.aqin.custom.propertyeditor;


public class Address {
   private String province;
   private String city;

   public String getProvince() {
      return province;
   }

   public void setProvince(String province) {
      this.province = province;
   }

   public String getCity() {
      return city;
   }

   public void setCity(String city) {
      this.city = city;
   }
}

AqinEntity.java

package com.aqin.custom.propertyeditor;

import org.springframework.stereotype.Component;


@Component
public class AqinEntity {
   private String id;
   private String name;
   private Address address;

   public String getId() {
      return id;
   }

   public void setId(String id) {
      this.id = id;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public Address getAddress() {
      return address;
   }

   public void setAddress(Address address) {
      this.address = address;
   }
}
2. 新建继承PropertyEditorSupport类的属性编辑器

MyPropertyEditor.java

package com.aqin.custom.propertyeditor;

import org.springframework.util.StringUtils;

import java.beans.PropertyEditorSupport;


public class MyPropertyEditor extends PropertyEditorSupport {

   @Override
   public void setAsText(String text) throws IllegalArgumentException {
      if (StringUtils.hasText(text)) {
         String[] addrs = text.split("_");
         Address address = new Address();
         address.setProvince(addrs[0]);
         address.setCity(addrs[1]);
         this.setValue(address);
         return;
      }
      throw new IllegalArgumentException("MyPropertyEditor: " + text + " could not convert to the target type!");
   }
}
3. 新建实现PropertyEditorRegistrar接口的属性编辑器注册器

MyPropertyEditorRegistrar.java

package com.aqin.custom.propertyeditor;

import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;


public class MyPropertyEditorRegistrar implements PropertyEditorRegistrar {

   @Override
   public void registerCustomEditors(PropertyEditorRegistry registry) {
      registry.registerCustomEditor(Address.class, new MyPropertyEditor());
   }

}
4. 新建配置文件

applicationContext2.xml




   
      
      
      
   

   
      
         
            
         
      
   
5. 修改启动类

加载新建的配置文件(applicationContext2.xml)

package com.aqin;

import com.aqin.custom.propertyeditor.AqinEntity;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class AqinApplication {
   public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");
      AqinEntity aqin = context.getBean(AqinEntity.class);
      System.out.println(aqin.getAddress().getProvince());
      System.out.println(aqin.getAddress().getCity());
   }
}
Debug瞅下流程

spring是在运行到哪里执行了我们的自定义编辑器和注册器的

我们在启动类和新建的属性编辑器和属性编辑器注册器中分别加上断点,如下面的一堆图 --->

AqinApplication.java

​MyPropertyEditor.java

​MyPropertyEditorRegistrar.java

​此外,还有2个地方需要加上断点:

找到AbstractBeanFactory类中的public void addPropertyEditorRegistrar(PropertyEditorRegistrar registrar)函数,如下图位置:

​AbstractApplicationContext类中的refresh()函数中的finishBeanFactoryInitialization(beanFactory)方法,如下图位置:

​然后,Debug启动 --->

​直接按resume project,在idea里长这样:

​有一点需要特别注意⚠️在第一次Debug到AbstractBeanFactory类中的public void addPropertyEditorRegistrar(PropertyEditorRegistrar registrar)函数时直接按resume project继续到下一断点,继续按resume project,直到第二次Debug到addPropertyEditorRegistrar函数,如下图:

​此时你会发现,这个registrar的变量就是我们自己新建的MyPropertyEditorRegistrar对象,再向下step over两步,自定义的MyPropertyEditorRegistrar已经被加进了系统的propertyEditorRegistrars(真棒┏ (^ω^)=!)

​继续按resume project走到下一个断点,finishBeanFactoryInitialization(beanFactory)

​继续按resume project

​进入下一个断点,会发现我们来到了我们准备期间创建的自定义属性编辑器注册器:

​step over一步,可以从下面的变量register中看到一开始定义的属性编辑器已经注册进了自定义的属性编辑器注册器中(好绕口(。 ˇ‸ˇ 。))

​使用idea快捷键往前倒一步(mac是command+左侧大括号),则回到registerCustomEditors

​然后如果你想刚清楚到底是怎么解析属性的,就一直按step over一步一步Debug,嫌麻烦的话可以直接按resume project进到下一个端点。

step over一直到AbstractAutowireCapableBeanFactory中的applyPropertyValues()函数,这里需要慢一点看喽~

​applyPropertyValues()函数中有个for循环,是遍历属性赋值的,看下图中间的那一行可以看到,此时字符串还未被解析为我们想要的类型

这里由于我们实体类中第三个字段才是Address,所以前两次循环,直接跳过就好,着重关注propertyValue的值为Addresss时,详情参考下图:

​继续向下step over,然后step into进入函数convertForProperty(...)

​继续向下step over,然后step into进入函数convertForProperty(value, propertyName)

​继续向下step over,然后step into进入函数convertForProperty(propertyName, null, value, td)

​继续向下step over,然后step into进入函数convertIfNecessary(propertyName, oldValue, newValue, td.getType(), td);

​继续向下step over,然后step into进入函数convertIfNecessary(propertyName, oldValue, newValue, requiredType, td);

继续向下step over,然后step into进入函数doConvertValue(oldValue, convertedValue, requiredType, editor)

​继续向下step over,然后step into进入函数doConvertTextValue(oldValue, newTextValue, editor)

继续向下step over,然后step into进入函数setAsText(newTextValue)

看!终于到了我们的重头戏~

到这里String已经转换成了Address对象W(`0`)W~~

看到了这~就可以直接继续执行完全部的代码了 ---> 出现下面的输出,我们的自定义类型编辑器就扩展成功啦✅撒花(*¯︶¯*)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/949262.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号