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 super關(guān)鍵字用法實(shí)戰(zhàn)案例分析
這篇文章主要介紹了Java super關(guān)鍵字用法,結(jié)合具體案例形式分析了java super關(guān)鍵字調(diào)用父類構(gòu)造方法、屬性及方法等相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-09-09
一個(gè)applicationContext 加載錯(cuò)誤導(dǎo)致的阻塞問題及解決方法
這篇文章主要介紹了一個(gè)applicationContext 加載錯(cuò)誤導(dǎo)致的阻塞問題及解決方法,需要的朋友可以參考下2018-11-11
解決maven build 無反應(yīng),直接terminated的問題
下面小編就為大家?guī)硪黄鉀Qmaven build 無反應(yīng),直接terminated的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
IDEA的部署設(shè)置改為war exploded運(yùn)行項(xiàng)目出錯(cuò)問題
在使用IDEA配置warexploded部署時(shí),可能會(huì)遇到路徑問題或404錯(cuò)誤,解決方法是進(jìn)入Deployment設(shè)置,刪除Application content中的/marry_war_exploded,使其為空,然后重新運(yùn)行項(xiàng)目即可,這是一種有效的解決策略,希望能幫助到遇到同樣問題的開發(fā)者2024-10-10
Mybatis查詢Sql結(jié)果未映射到對應(yīng)得實(shí)體類上的問題解決
使用mybatis查詢表數(shù)據(jù)得時(shí)候,發(fā)現(xiàn)對應(yīng)得實(shí)體類字段好多都是null,本文主要介紹了Mybatis查詢Sql結(jié)果未映射到對應(yīng)得實(shí)體類上的問題解決,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02
SpringBoot整合Web之CORS支持與配置類和 XML配置及注冊攔截器
這篇文章主要介紹了SpringBoot整合Web開發(fā)中CORS支持與配置類和 XML配置及注冊攔截器的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08

