Spring 自定义propertyEditor的示例代码

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Spring 自定义propertyEditor的示例代码

Acaak   2022-12-13 我要评论

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>

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们