亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Spring自定義屬性編輯器CustomEfitorConfigurer

系統 2039 0

假設有如下兩個類:

?

package ?customerPropertyEditor;

public ? class ?Contact? ... {
??
private ?PhoneNumber?phoneNumber;

public ?PhoneNumber?getPhoneNumber()? ... {
????
return ?phoneNumber;
}


public ? void ?setPhoneNumber(PhoneNumber?phoneNumber)? ... {
????
this .phoneNumber? = ?phoneNumber;
}

}



package ?customerPropertyEditor;

public ? class ?PhoneNumber? ... {
??
private ?String?areaCode;
??
private ?String?prefix;
??
private ?String?number;
??
public ?PhoneNumber() ... {
??????
??}

??
public ?PhoneNumber(String?areaCode,String?prefix,String?number) ... {
??????
this .areaCode = areaCode;
??????
this .prefix = prefix;
??????
this .number = number;
??}

public ?String?getAreaCode()? ... {
????
return ?areaCode;
}

public ? void ?setAreaCode(String?areaCode)? ... {
????
this .areaCode? = ?areaCode;
}

public ?String?getNumber()? ... {
????
return ?number;
}

public ? void ?setNumber(String?number)? ... {
????
this .number? = ?number;
}

public ?String?getPrefix()? ... {
????
return ?prefix;
}

public ? void ?setPrefix(String?prefix)? ... {
????
this .prefix? = ?prefix;
}

public ?String?toString() ... {
????
return ? this .getAreaCode() + " - " + this .getPrefix() + " - " + this .getNumber();
}

}

?我們想裝配一個持久一個phoneNumber對象的contact,需要做如下配置

?

< bean? id ="phoneNumnber" ?class ="......." >
?????
< constructor-arg? index ="0" >
??????????
< value > 800 </ value >
????
</ constructor-arg >
??
< constructor-arg? index ="1" >
??????????
< value > 810 </ value >
????
</ constructor-arg >
??
< constructor-arg? index ="2" >
??????????
< value > 8181 </ value >
????
</ constructor-arg >
</ bean >

< bean? id ="contact" ?class ="....." >
???
< property? value ="phoneNumber" >
??????
< ref? bean ="phoneNumnber" />
??
</ property >
</ bean >

?

使用Spring提供的自定義屬性編輯器功能(其實是spring支持java.beans.PropertyEditorSupport)

我們可以用這樣的方式來裝配contact

?

? < bean? id ="contact" ?class ="customerPropertyEditor.Contact" >
???
< property? name ="phoneNumber" >
????
< value > 800-810-8181 </ value >
???
</ property >
?
</ bean >


是不是方便了許多呢?

首先我們需要自定一個屬性編輯器,并返回一個裝配好的PhoneNumnber對象

這里要類名命名規范,如果我們需要自定義編輯器操作的類名為PhoneNumber,我們編輯器類名就是PhoneNumberEditor

package ?customerPropertyEditor;

import ?java.beans.PropertyEditorSupport;

public ? class ?PhoneNumberEditor? extends ?PropertyEditorSupport? ... {


????
public ? void ?setAsText(String?text)? throws ?IllegalArgumentException? ... {
????????String?stripped
= convertToNumberic(text);
????????String?areaCode
= stripped.substring( 0 , 3 );
????????String?prefix
= stripped.substring( 3 , 6 );
????????String?number
= stripped.substring( 6 );
????????PhoneNumber?phoneNumber
= new ?PhoneNumber(areaCode,prefix,number);
????????setValue(phoneNumber);
????????
????}

????
public ?String?convertToNumberic(String?str) ... {
????????StringBuffer?buffer
= new ?StringBuffer();
????????
for ( int ?i = 0 ;i < str.length();i ++ ) ... {
????????????
char ?c = str.charAt(i);
????????????
if (Character.isDigit(c)) ... {
????????????????buffer.append(c);
????????????}

????????}

????????
return ?buffer.toString();
????}


}

?

接下來,在spring配置文件中注冊自定義編輯器

注意CustomEfitorConfigurer特別容易寫成CustomerEfitorConfigurer

? < bean? id ="customEditorConfigurer" ?class ="org.springframework.beans.factory.config.CustomEditorConfigurer" >
???
< property? name ="customeEditors" >
?????
< map >
???????
< entry? key ="customerPropertyEditor.PhoneNumber" >
?????????
< bean? id ="phoneNumberEditor" ?class ="customerPropertyEditor.PhoneNumberEditor" />
???????
</ entry >
?????
</ map >
???
</ property >
?
</ bean >

?

測試代碼:

?

package ?customerPropertyEditor;

import ?java.io.File;
import ?org.springframework.beans.factory.BeanFactory;
import ?org.springframework.beans.factory.xml.XmlBeanFactory;
import ?org.springframework.core.io.FileSystemResource;

public ? class ?TestCustomerPropertyEditor? ... {
????
public ? static ? void ?main(String[]?args)? ... {
????????String?filePath
= System.getProperty( " user.dir " ) + File.separator + " customerPropertyEditor " + File.separator + " hello.xml " ;????
????????BeanFactory?factory
= new ?XmlBeanFactory( new ?FileSystemResource(filePath));????????????
????????Contact?contact
= (Contact)factory.getBean( " contact " );
????????System.out.println(contact.getPhoneNumber());
????}

}

?

運行結果:

800-810-8181



Spring自定義屬性編輯器CustomEfitorConfigurer使用實例


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产全黄一级毛片 | 四虎永久在线精品 | 免费一级欧美片在线观看 | 国产亚洲在线 | 两性色午夜视频免费老司机 | 国产精品中文字幕在线 | 婷婷四房 | 亚洲成人视 | 干夜夜| 荔枝污 | 欧美亚洲中日韩中文字幕在线 | 中文字幕51精品乱码在线 | 日韩欧美亚洲中字幕在线播放 | s8国产成人精品视频 | 久国产视频 | 天天操天天操天天操天天操 | 337p色噜噜 | 国内精品美女久久久久 | 久久午夜伦理 | 九九热中文字幕 | 一级毛片免费播放 | 久久狠狠婷婷丁香香蕉 | 狠狠操天天操夜夜操 | 久久久精品视频在线观看 | 日韩 欧美 亚洲 中文字幕 | 亚洲国产精品不卡毛片a在线 | 青青青青青国产费线在线观看 | 国产免费专区 | 热久久在线观看 | 99久久精品国产国产毛片 | 曰本色wa| 国产一级特黄高清免费大片 | 国产日韩不卡免费精品视频 | 日本免费一区二区三区a区 日本免费一区二区三区看片 | 亚洲不卡视频在线观看 | 二级毛片| 久久最近最新中文字幕大全 | 亚洲国产爱 | 亚洲精品国自产拍影院 | 深夜影院a | 米奇久久 |