Spring中如何自定义一个属性编辑器呢?

欣喜 Spring 发布时间:2024-11-22 15:36:52 阅读数:12784 1
下文笔者讲述Spring中自定义属性编辑器的方法及示例分享,如下所示
1.定义一个 Editor 继承  PropertyEditorSupport 重写其中的setAsText方法
2.定义一个  CustomPropertyEditorRegistrar 实现 registerCustomEditors ,
   将CustomExtend.class和CustomExtendPropertyEditor进行关联
3.使用 BeanFactoryPostProcessor 
   将 CustomPropertyEditorRegistrar 注册到BeanFactory中
例:
Custom 

package com.jd.customEditor;

public class Custom {

    private  String cusId;
    private  String cusTel;
    private  CustomExtend customExtend;


    public String getCusId() {
        return cusId;
    }

    public void setCusId(String cusId) {
        this.cusId = cusId;
    }

    public String getCusTel() {
        return cusTel;
    }

    public void setCusTel(String cusTel) {
        this.cusTel = cusTel;
    }

    public CustomExtend getCustomExtend() {
        return customExtend;
    }

    public void setCustomExtend(CustomExtend customExtend) {
        this.customExtend = customExtend;
    }


    public String toString(){
    	return "Custom [cusId=" + cusId + ", cusTel=" + cusTel + ", customExtend=" + customExtend + "]";
    }
}


CustomExtend


package com.jd.customEditor;

public class CustomExtend {

    private String name;
    private String email;
    private String hobby;

    public String getName() {
        return name;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }

    @Override
    public String toString() {
        return "CustomExtend{" +
                "name='" + name + '\'' +
                ", email='" + email + '\'' +
                ", hobby='" + hobby + '}';
    }
}


CustomExtendPropertyEditor

package com.jd.customEditor;

import java.beans.PropertyEditorSupport;

public class CustomExtendPropertyEditor extends PropertyEditorSupport {


    public void setAsText(String text) throws IllegalArgumentException {

        //使用下划线分割字符串
        // 传入字符串格式:name_hobby_email
        String[] strs = text.split("_");
        CustomExtend customExtend = new CustomExtend();
        if(strs.length >0) {
            customExtend.setName(strs[0]);
        }

        if(strs.length >1) {
            customExtend.setHobby(strs[1]);
        }


        if(strs.length >2) {
            customExtend.setEmail(strs[2]);
        }
        setValue(customExtend);
    }
}


CustomPropertyEditorRegistrar


package com.jd.customEditor;

import org.springframework.beans.PropertyEditorRegistrar;


public class CustomPropertyEditorRegistrar implements PropertyEditorRegistrar {

    public void registerCustomEditors(org.springframework.beans.PropertyEditorRegistry registry) {
        registry.registerCustomEditor(CustomExtend.class, new CustomExtendPropertyEditor());
    }
}


MyBeanFactoryPostProcess


package com.jd.customEditor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurablelistableBeanFactory;

public class MyBeanFactoryPostProcess implements BeanFactoryPostProcessor {


    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        configurableListableBeanFactory.addPropertyEditorRegistrar(new CustomPropertyEditorRegistrar());
    }
}


customEditor.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation=
               "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="custom" class="com.jd.customEditor.Custom" >
        <property name="cusId" value="1" />
        <property name="cusTel" value="123456789" />
        <property name="customExtend"  value="11_" />
    </bean>

    <bean class="com.jd.customEditor.MyBeanFactoryPostProcess" name="myBeanFactoryPostProcess" >
    </bean>


</beans>

运行效果
自定义属性编辑器
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaFramework/Spring/202411/8187.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者