`

spring 注册用户自定义的PropertyEditor

阅读更多
当以一个字符串值来设置bean属性时,Spring IoC 容器最终使用标准的JavaBean PropertyEditor来将这些字符串转化成复杂的数据类型。Spring预先注册了一些PropertyEditor(举例来说,将一个以字符串表示的Class转化成Class对象)。除此之外,Java标准的JavaBean PropertyEditor>会识别在同一包结构下的类和它对应的命名恰当的Editor,并自动将其作为这个类的的Editor。

如果你想注册自己定义的PropertyEditor,那么有几种不同的机制供君选择。其中,最原始的手工方式是在你有一个BeanFactory的引用实例时,使用ConfigurableBeanFactory的registerCustomEditor()方法。当然,通常这种方法不够方便,因而并不推荐使用。另外一个简便的方法是使用一个称之为CustomEditorConfigurer的特殊的bean factory后置处理器。尽管bean factory的后置处理器可以半手工化的与BeanFactory实现一起使用,但是它存在着一个嵌套属性的建立方式。因此,强烈推荐的一种做法是与ApplicationContext一起来使用它。这样就能使之与其他的bean一样以类似的方式部署同时被容器所感知并使用。

注意所有的bean factory和application context都会自动地使用一系列的内置属性编辑器,通过BeanWrapper来处理属性的转化。在这里列出一些在BeanWrapper中注册的标准的属性编辑器。除此之外,ApplicationContext覆盖了一些默认行为,并为之增加了许多编辑器来处理在某种意义上合适于特定的application context类型的资源查找。

标准的JavaBean的PropertyEditor>实例将以String表示的值转化成实际复杂的数据类型。CustomEditorConfigurer作为一个bean factory的后置处理器, 能够便捷地将一些额外的PropertyEditor>实例加入到ApplicationContext中去。

考虑用户定义的类ExoticType和DependsOnExoticType,其中,后者需要将前者设置为它的属性:
package example;
		
public class ExoticType {

    private String name;

    public ExoticType(String name) {
        this.name = name;
    }
}

public class DependsOnExoticType { 
   
    private ExoticType type;

    public void setType(ExoticType type) {
        this.type = type;
    }
}

在一切建立起来以后,我们希望通过指定一个字符串来设置type属性的值,然后PropertyEditor>将在幕后帮你将其转化为实际的ExoticType对象:
<bean id="sample" class="example.DependsOnExoticType">
    <property name="type" value="aNameForExoticType"/>
</bean>

PropertyEditor>的实现看上去就像这样:
// converts string representation to ExoticType object
package example;

public class ExoticTypeEditor extends PropertyEditorSupport {

    private String format;

    public void setFormat(String format) {
        this.format = format;
    }
    
    public void setAsText(String text) {
        if (format != null && format.equals("upperCase")) {
            text = text.toUpperCase();
        }
        ExoticType type = new ExoticType(text);
        setValue(type);
    }
}

最后,我们通过使用CustomEditorConfigurer来为ApplicationContext注册一个新的PropertyEditor>,这样,我们就可以在任何需要的地方使用它了:
<bean id="customEditorConfigurer" 
    class="org.springframework.beans.factory.config.CustomEditorConfigurer">
  <property name="customEditors">
    <map>
      <entry key="example.ExoticType">
        <bean class="example.ExoticTypeEditor">
          <property name="format" value="upperCase"/>
        </bean>
      </entry>
    </map>
  </property>
</bean>
分享到:
评论

相关推荐

    学习Spring必学的Java基础知识(3)—PropertyEditor

    NULL 博文链接:https://bijian1013.iteye.com/blog/2164121

    PropertyEditor

    又一个中文转unicode的工具,在创建struts资源文件的时候,非常有用,输入中文后,直接保存就自动转换了

    spring.net中文手册在线版

    4.12.1.注册自定义解析器 4.12.2.创建自定义资源处理器 4.12.3.配置类型别名 4.12.4.注册类型转换器 4.13.IApplicationContext接口的扩展功能 4.13.1.上下文继承 4.13.2.使用IMessageSource接口 4.13.3.在Spring.NET...

    Spring-Reference_zh_CN(Spring中文参考手册)

    注册用户自定义的PropertyEditor 6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 6.1.3. Spring的AOP代理 6.2. @AspectJ支持 6.2.1. 启用@AspectJ支持 6.2.2. 声明...

    Spring 2.0 开发参考手册

    5.4.2. 内建的PropertyEditor实现 6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 6.1.3. Spring的AOP代理 6.2. @AspectJ支持 6.2.1. 启用@AspectJ支持 ...

    Spring中文帮助文档

    5.4.2. 内建的PropertyEditor实现 6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 6.1.3. AOP代理 6.2. @AspectJ支持 6.2.1. 启用@AspectJ支持 6.2.2. 声明...

    Spring API

    5.4.2. 内建的PropertyEditor实现 6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 6.1.3. AOP代理 6.2. @AspectJ支持 6.2.1. 启用@AspectJ支持 6.2.2. 声明...

    spring chm文档

    5.4.2. 内建的PropertyEditor实现 6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 6.1.3. Spring的AOP代理 6.2. @AspectJ支持 6.2.1. 启用@AspectJ支持 ...

    开源框架 Spring Gossip

    实作 Validator 使用 PropertyEditor 档案上传 &lt;br&gt; &lt;br&gt;View层方案、Web框架整合 当使用JSP作为View层技术时,您可以结合JSTL以及Spring提供的标签,而除了JSP技术作为View层之外,Spring还...

    Spring MVC 3.0实战指南.ppt

    基于ConversionService体系,定义自定义的类型转换器 格式化:带格式字符串内部对象 相互转换 使用支持格式化的转换器 数据校验框架 JSR 303 数据校验框架 如何使用注解驱动的校验 使用校验功能时,处理方法要...

    Spring3MVC注解教程.ppt

    《Spring MVC 3.0实战指南》,参考《Spring 3.x企业应用开发实战》。 内容简介: 1、Spring MVC框架简介 2、HTTP请求地址映射 3、HTTP请求数据的绑定 4、数据转换、格式化、校验 5、数据模型控制 6、视图及...

    spring-framework-reference-4.1.2

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    Spring Boot项目中定制PropertyEditors方法

    在本篇文章里小编给大家分享的是一篇关于Spring Boot定制PropertyEditors的知识点内容,有需要的朋友们可以参考学习下。

    SpringMVC之DataBinding和Validation--Validator,PropertyEditor,Converter,Formatter

    NULL 博文链接:https://b-l-east.iteye.com/blog/1705872

    Property Editor

    properties Editor for eclipse,解压后丢到eclipse安装目录的drops下重启eclipse即可,亲测有效。

    spring-framework-reference4.1.4

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    XAF实现子导航栏

    xaf,和我博客中使用PropertyEditor实现子导航栏不同,这里使用自定义模版实现一个子导航,更为优雅

    matlab GUI设计命令大全

    (3)对象属性编辑器(PropertyEditor):可查看每个对象的属性 值,也可修改、设置对象的属性值;(4)位置调整工具(Alignment Too1):用来调整图形窗1:1中各个图形对象的位置的工具. (5)对象浏览编辑器(Object Browser...

    Delphi7~Delphi2010 JSON 读写组件(源码)

    Delphi7~Delphi2010 JSON 读写组件(源码)

    superobjectv1.2.4.zip_DELPHI JSON格式解析_JSON_json delphi_superobje

    DELPHI JSON 解析字符串,封装

Global site tag (gtag.js) - Google Analytics