package com.stamen.propedit; import org.apache.commons.lang.builder.ToStringBuilder; public class Address { private String street; private String doorNum; private String postCode; public String getDoorNum() { return doorNum; } public void setDoorNum(String doorNum) { this.doorNum = doorNum; } public String getPostCode() { return postCode; } public void setPostCode(String postCode) { this.postCode = postCode; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String toString() { return ToStringBuilder.reflectionToString(this); } }
Spring为一般的属性类型提供了默认的编辑器,BeanWrapperImpl是Spring框架中重要的类,它负责对注入的Bean进行包装化的管理,常见属性类型对应的编辑器即在该类中通过以下代码定义:
private void registerDefaultEditors() { // Simple editors, without parameterization capabilities. // The JDK does not contain a default editor for any of these target types. this.defaultEditors.put(byte[].class, new ByteArrayPropertyEditor()); this.defaultEditors.put(Class.class, new ClassEditor()); this.defaultEditors.put(File.class, new FileEditor()); this.defaultEditors.put(InputStream.class, new InputStreamEditor()); this.defaultEditors.put(Locale.class, new LocaleEditor()); this.defaultEditors.put(Properties.class, new PropertiesEditor()); this.defaultEditors.put(Resource[].class, new ResourceArrayPropertyEditor()); this.defaultEditors.put(String[].class, new StringArrayPropertyEditor()); this.defaultEditors.put(URL.class, new URLEditor()); // Default instances of collection editors. // Can be overridden by registering custom instances of those as custom editors. this.defaultEditors.put(Collection.class, new CustomCollectionEditor(Collection.class)); this.defaultEditors.put(Set.class, new CustomCollectionEditor(Set.class)); this.defaultEditors.put(SortedSet.class, new CustomCollectionEditor(SortedSet.class)); this.defaultEditors.put(List.class, new CustomCollectionEditor(List.class)); // Default instances of character and boolean editors. // Can be overridden by registering custom instances of those as custom editors. PropertyEditor characterEditor = new CharacterEditor(false); PropertyEditor booleanEditor = new CustomBooleanEditor(false); // The JDK does not contain a default editor for char! this.defaultEditors.put(char.class, characterEditor); this.defaultEditors.put(Character.class, characterEditor); // Spring's CustomBooleanEditor accepts more flag values than the JDK's default editor. this.defaultEditors.put(boolean.class, booleanEditor); this.defaultEditors.put(Boolean.class, booleanEditor); // The JDK does not contain default editors for number wrapper types! // Override JDK primitive number editors with our own CustomNumberEditor. PropertyEditor byteEditor = new CustomNumberEditor(Byte.class, false); PropertyEditor shortEditor = new CustomNumberEditor(Short.class, false); PropertyEditor integerEditor = new CustomNumberEditor(Integer.class, false); PropertyEditor longEditor = new CustomNumberEditor(Long.class, false); PropertyEditor floatEditor = new CustomNumberEditor(Float.class, false); PropertyEditor doubleEditor = new CustomNumberEditor(Double.class, false); this.defaultEditors.put(byte.class, byteEditor); this.defaultEditors.put(Byte.class, byteEditor); this.defaultEditors.put(short.class, shortEditor); this.defaultEditors.put(Short.class, shortEditor); this.defaultEditors.put(int.class, integerEditor); this.defaultEditors.put(Integer.class, integerEditor); this.defaultEditors.put(long.class, longEditor); this.defaultEditors.put(Long.class, longEditor); this.defaultEditors.put(float.class, floatEditor); this.defaultEditors.put(Float.class, floatEditor); this.defaultEditors.put(double.class, doubleEditor); this.defaultEditors.put(Double.class, doubleEditor); this.defaultEditors.put(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, false)); this.defaultEditors.put(BigInteger.class, new CustomNumberEditor(BigInteger.class, false)); }
package com.stamen.propedit; import org.apache.commons.lang.builder.ToStringBuilder; public class Person { private String name; private Address address; public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString() { return ToStringBuilder.reflectionToString(this); } }
package com.stamen.propedit; import org.apache.commons.lang.builder.ToStringBuilder; public class Address { private String street; private String doorNum; private String postCode; public String getDoorNum() { return doorNum; } public void setDoorNum(String doorNum) { this.doorNum = doorNum; } public String getPostCode() { return postCode; } public void setPostCode(String postCode) { this.postCode = postCode; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String toString() { return ToStringBuilder.reflectionToString(this); } }AddressPropertyEditor.java
package com.stamen.propedit; import java.beans.PropertyEditorSupport; import java.util.Date; import org.springframework.util.StringUtils; public class AddressPropertyEditor extends PropertyEditorSupport{ //支持的格式为 streeValue,doorNumValue,postCode public void setAsText(String text) { System.out.println("使用自己的编辑器。"); if (text == null || !StringUtils.hasText(text)) { throw new IllegalArgumentException("老大,不能为空啊!"); } else { String[] strArr = StringUtils.tokenizeToStringArray(text,","); Address add = new Address(); add.setStreet(strArr[0]); add.setDoorNum(strArr[1]); add.setPostCode(strArr[2]); setValue(add); } } public String getAsText() { Address add = (Address)getValue(); return ""+add; } }
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="com.stamen.propedit.Address"> <!-- 属性类型 --> <bean class="com.stamen.propedit.AddressPropertyEditor"/> <!--对应Address的编辑器 --> </entry> </map> </property> </bean> <bean id="person" class="com.stamen.propedit.Person"> <property name="name" value="Tom"/> <property name="address" value="朝阳区,Soho 1601,010101"/> </bean>