Spring?自定義propertyEditor的示例代碼
更新時間:2022年12月13日 10:51:39 作者:Acaak
這篇文章主要介紹了Spring?自定義propertyEditor的示例代碼,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下


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構(gòu)造方法Vector創(chuàng)建對象源碼分析
這篇文章主要介紹了java構(gòu)造函數(shù)中對Vector源碼及原理的分析,有需要的朋友可以借鑒參考下,希望可以有所幫助,祝大家早日升職加薪2021-09-09
Spring?MVC中JSON數(shù)據(jù)處理方式實戰(zhàn)案例
Spring MVC是個靈活的框架,返回JSON數(shù)據(jù)的也有很多五花八門的方式,下面這篇文章主要給大家介紹了關(guān)于Spring?MVC中JSON數(shù)據(jù)處理方式的相關(guān)資料,需要的朋友可以參考下2024-01-01
SpringBoot Mybatis如何配置多數(shù)據(jù)源并分包
這篇文章主要介紹了SpringBoot Mybatis如何配置多數(shù)據(jù)源并分包,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-05-05
Spring?Cloud?Gateway中netty線程池優(yōu)化示例詳解
這篇文章主要介紹了Spring?Cloud?Gateway中netty線程池優(yōu)化示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

