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>
到此這篇關(guān)于Spring 自定義propertyEditor的文章就介紹到這了,更多相關(guān)Spring 自定義propertyEditor內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java設(shè)計(jì)模式中裝飾者模式應(yīng)用詳解
裝飾者模式:在不改變原有對象的基礎(chǔ)之上,動態(tài)的將功能附加到對象上,提供了繼承更有彈性的替代方案,也體現(xiàn)了開閉原則。本文將通過示例詳細(xì)講解一下裝飾者模式,需要的可以參考一下2022-11-11基于java構(gòu)造方法Vector創(chuàng)建對象源碼分析
這篇文章主要介紹了java構(gòu)造函數(shù)中對Vector源碼及原理的分析,有需要的朋友可以借鑒參考下,希望可以有所幫助,祝大家早日升職加薪2021-09-09SpringBoot 如何實(shí)時(shí)刷新靜態(tài)文件
這篇文章主要介紹了SpringBoot如何實(shí)時(shí)刷新靜態(tài)文件,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12Spring?MVC中JSON數(shù)據(jù)處理方式實(shí)戰(zhàn)案例
Spring MVC是個靈活的框架,返回JSON數(shù)據(jù)的也有很多五花八門的方式,下面這篇文章主要給大家介紹了關(guān)于Spring?MVC中JSON數(shù)據(jù)處理方式的相關(guān)資料,需要的朋友可以參考下2024-01-01SpringBoot Mybatis如何配置多數(shù)據(jù)源并分包
這篇文章主要介紹了SpringBoot Mybatis如何配置多數(shù)據(jù)源并分包,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05Spring?Cloud?Gateway中netty線程池優(yōu)化示例詳解
這篇文章主要介紹了Spring?Cloud?Gateway中netty線程池優(yōu)化示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07