java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Spring 自定义propertyEditor

Spring 自定义propertyEditor的示例代码

作者:Acaak

这篇文章主要介绍了Spring 自定义propertyEditor的示例代码,本文结合示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

User

package com.example.zylspringboot.selfEditor;

public class User {

    private String name;
    private Address address;
    private Integer age;

    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;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", address=" + address +
                ", age=" + age +
                '}';
    }
}

Address

package com.example.zylspringboot.selfEditor;

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;
    }

    @Override
    public String toString() {
        return "Address{" + "province='" + province + '\'' + ", city='" + city + '\'' + '}';
    }
}

SelfPropertyEditor

package com.example.zylspringboot.selfEditor;

import java.beans.PropertyEditorSupport;

public class SelfPropertyEditor extends PropertyEditorSupport {

    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        String[] s = text.split("_");
        Address address = new Address();
        address.setCity(s[0]);
        address.setProvince(s[1]);
        super.setValue(address);
    }
}

AcaakPropertyRegistor

package com.example.zylspringboot.selfEditor;

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

public class AcaakPropertyRegistor implements PropertyEditorRegistrar {
    @Override
    public void registerCustomEditors(PropertyEditorRegistry propertyEditorRegistry) {
        propertyEditorRegistry.registerCustomEditor(Address.class,new SelfPropertyEditor());
    }
}

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"
       xmlns:context="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="user" class="com.example.zylspringboot.selfEditor.User">
        <property name="age" value="18"></property>
        <property name="name" value="Acaak"></property>
        <property name="address" value="广东省_广州市"></property>
    </bean>

    <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
        <property name="propertyEditorRegistrars">
            <list>
                <bean class="com.example.zylspringboot.selfEditor.AcaakPropertyRegistor"></bean>
            </list>
        </property>
    </bean>
	或
	<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
        <property name="customEditors">
            <map>
                <entry key="com.example.zylspringboot.selfEditor.Address">
                    <value>com.example.zylspringboot.selfEditor.SelfPropertyEditor</value>
                </entry>
            </map>
        </property>
    </bean>
</beans>

到此这篇关于Spring 自定义propertyEditor的文章就介绍到这了,更多相关Spring 自定义propertyEditor内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文