Spring中我们可以使用属性编辑器来将特定的字符串转换为对象
String--转换-->object

java.beans.PropertyEditor(JDK中的接口)用于将xml文件中字符串转换为特定的类型,同时JDK为我们提供一个实现类java.beans.PropertyEditorSupport

Spring在注入时,如果遇到类型不一致(例如需要Address类型但是用户传了个String)则会去调用相应的属性编辑器进行转换

spring会调用属性编辑器的setAsText(String str)进行处理用户传的字符串,并调用getValue()方法获取处理后得到的对象
在代码中处理完后需要调用setValue方法,要不然spring调用getValue方法拿不到处理后转换成的对象

自定义属性编辑器示例:
注意:在配置文件中CustomEditorConfigurer类的使用,在htmlsingle中直接搜索类名即可

Address类

  1. public class Address {
  2. private String city;
  3. private String street;
  4. private String country;
  5. set/get
  6. .....
  7. }

Student类

  1. public class Student {
  2. private long id;
  3. private String name;
  4. private boolean gender;
  5. private int age;
  6. private Address address;
  7. get/set
  8. ...
  9. }

自定义编辑器类

  1. public class AddressEditor extends PropertyEditorSupport {
  2.  
  3. @Override
  4. public String getAsText() {
  5. return super.getAsText();
  6. }
  7.  
  8. //Spring遇到数据类型不一致并且不能自己处理的时候会调用这个方法处理字符串
  9. @Override
  10. public void setAsText(String text) throws IllegalArgumentException {
  11. String[] str = text.split(",");
  12. String city = str[0];
  13. String street = str[1];
  14. String country = str[2];
  15. Address add = new Address(city, street, country);
  16. setValue(add);
  17. }
  18.  
  19. }

xml文件:

  1. <!-- 这个配置指明哪个类型对应哪个自定义编辑器 -->
  2. <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
  3. <property name="customEditors">
  4. <map>
  5. <entry key="com.briup.ioc.proEdit.Address" value="com.briup.ioc.proEdit.AddressEditor"/>
  6. </map>
  7. </property>
  8. </bean>
  9.  
  10. <!-- spring发现address的类型是Address的时候,就会调用对应的属性编辑器处理AddressEditor了 -->
  11. <bean id="student" class="com.briup.ioc.proEdit.Student">
  12. <property name="id" value="1"/>
  13. <property name="name" value="tom"/>
  14. <property name="age" value="45"/>
  15. <property name="gender" value="true"/>
  16. <property name="address">
  17. <value>kunshan,xueyuan,China</value>
  18. </property>
  19. </bean>

这个注册编辑器的操作在高版本的Spring框架中可以不写,这与框架的版本有关

SpringIOC自定义属性编辑器PropertyEditor的更多相关文章

  1. Spring——自定义属性编辑器+Bean的生存范围+Bean的生命周期

    一.自定义属性编辑器(一个类): 步骤: 1.写一个类,这个类继承PropertyEditorSupport. 2.重写setAsText()方法. 3.在bean.xml文件中添加属性编辑器的bea ...

  2. [置顶] Spring中自定义属性编辑器

    Spring中的属性编辑器能够自动的将String类型转化成需要的类型,例如一个类里面的一个整型属性,在配置文件中我们是通过String类型的数字进行配置的,这个过程中就需要一个转化操作,当然这个转化 ...

  3. Spring自定义属性编辑器及原理解释.md

    bean的自动装配解释 手动解决方式 自动注入解决方式 bean的自动装配解释 之前有构造注入和设值注入,但是也是手动的 autowire ="byname" 这里要注意自动装配的 ...

  4. sprin源码解析之属性编辑器propertyEditor

    目录 异常信息 造成此异常的原因 bean 配置文件 调用代码 特别说明: 异常解决 注册springt自带的属性编辑器 CustomDateEditor 控制台输出 属性编辑器是何时并如何被注册到s ...

  5. spring源码解析之属性编辑器propertyEditor

    异常信息造成此异常的原因bean配置文件调用代码特别说明:异常解决注册springt自带的属性编辑器 CustomDateEditor控制台输出属性编辑器是何时并如何被注册到spring容器中的?查看 ...

  6. 0003 - 基于xml的Spring Bean 的创建过程

    一.目录 前言 创建 Bean 容器 加载 Bean 定义 创建 Bean Spring Bean 创建过程中的设计模式 总结 二.前言 2.1 Spring 使用配置 ApplicationCont ...

  7. Spring 属性注入(一)JavaBean 内省机制在 BeanWrapper 中的应用

    Spring 属性注入(一)JavaBean 内省机制在 BeanWrapper 中的应用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/101174 ...

  8. (spring-第13回【IoC基础篇】)PropertyEditor(属性编辑器)--实例化Bean的第五大利器

    上一篇讲到JavaBeans的属性编辑器,编写自己的属性编辑器,需要继承PropertyEditorSupport,编写自己的BeanInfo,需要继承SimpleBeanInfo,然后在BeanIn ...

  9. SpringMVC类型转换器、属性编辑器

    对于MVC框架,参数绑定一直觉得是很神奇很方便的一个东西,在参数绑定的过程中利用了属性编辑器.类型转换器 参数绑定流程 参数绑定:把请求中的数据,转化成指定类型的对象,交给处理请求的方法 请求进入到D ...

随机推荐

  1. luaj使用 方法签名规则 Cocos2dxLuaJavaBridge

    function AndroidHandler:getParamJson()     local args = {nil}     local ok,ret = luaj.callStaticMeth ...

  2. RobotFramework Selenium2 EXCUTE JAVASCRIPT

    https://www.cnblogs.com/lixy-88428977/p/9563247.html

  3. 尝试 zabbix 小记

    server : Ubuntu 16.04 zabbix: 2.2.23源码包 安装 gcc,curl,make,snmp 软件和zabbix依赖一些php 扩展包 sudo apt-get inst ...

  4. laravel sql mode only_full_group_by 解决小记

    環境: mysql: 5.7.* Laravel: 5.4.* sql 中使用到了 group by,會提示 500錯誤,將 config/database.php中的 strict的值改爲true, ...

  5. Qt Creator配置

    1.安装Git sudo apt install git 2.配置Git 用户和邮箱: git config --global user.name "xxx" git config ...

  6. [JZOJ3235] 数字八

    题目 题目大意 给你一个二维的图,其中.代表完好,*代表有缺陷. 现在要在图上刻一个数字\(8\),满足: 由两个矩形组成. 每个矩形中必须有空隙在内部,也就是说,至少为\(3*3\)的矩形. 上矩形 ...

  7. Windows 虚拟机 VM

    VMware是全球台式电脑及资料中心虚拟化解决方案的领导厂商.VMWare Workstation是该公司出品的“虚拟 PC”软件(即:大家常说的“虚拟机”),通过它可在一台电脑上同时运行更多的Mic ...

  8. Flink常用资料网址

    Flink官网https://flink.apache.org/ 阿里flink开发文档 https://help.aliyun.com/product/45029.html?spm=a2c4g.11 ...

  9. Mybatis笔记 - 原始Dao开发方法

    使用Mybatis开发Dao,通常有两个方法,即原始Dao开发方法和Mapper接口开发方法.原始Dao的开发方式是基于入门程序的基础上,对 控制程序 进行分层开发,程序员需要 编写 Dao接口 和 ...

  10. JAVA 设计模式之 原型模式详解

    原型模式(Prototype Pattern)是指原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. 原型模式利用的是克隆的原理,创建新的对象,JDK提供的Cloneable 和JSON. ...