假设有如下两个类:
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();
}
}
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 >
< 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 >
< 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();
}
}
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 >
< 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());
}
}
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