javaBean中的PropertyEditor,扩展PropertyEditorSupport类,可以简化自定义PropertyEditor
目的:将字符串转化为正确的类型
注意:从spring3开始,spring引入了更简单和结构良好的类型转化API(Type Conversion API)和字段格式化SPI(Field Formatting Service Provider Interface)(第10章)
样例
代码结构图独立应用程序中CustomDateEditor和StringTrimmerEditor这两个编辑器默认情况下并未在spring中注册
package com.apress.prospring5.ch4;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.*;
import java.util.regex.Pattern;
import java.text.SimpleDateFormat;
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.context.support.GenericXmlApplicationContext;
public class PropertyEditorBean {
private byte[] bytes; // ByteArrayPropertyEditor
private Character character; //CharacterEditor
private Class cls; // ClassEditor
private Boolean trueOrFalse; // CustomBooleanEditor
private List<String> stringList; // CustomCollectionEditor
private Date date; // CustomDateEditor
private Float floatValue; // CustomNumberEditor
private File file; // FileEditor
private InputStream stream; // InputStreamEditor
private Locale locale; // LocaleEditor
private Pattern pattern; // PatternEditor
private Properties properties; // PropertiesEditor
private String trimString; // StringTrimmerEditor
private URL url; // URLEditor
public void setCharacter(Character character) {
System.out.println("Setting character: " + character);
this.character = character;
}
public void setCls(Class cls) {
System.out.println("Setting class: " + cls.getName());
this.cls = cls;
}
public void setFile(File file) {
System.out.println("Setting file: " + file.getName());
this.file = file;
}
public void setLocale(Locale locale) {
System.out.println("Setting locale: " + locale.getDisplayName());
this.locale = locale;
}
public void setProperties(Properties properties) {
System.out.println("Loaded " + properties.size() + " properties");
this.properties = properties;
}
public void setUrl(URL url) {
System.out.println("Setting URL: " + url.toExternalForm());
this.url = url;
}
public void setBytes(byte... bytes) {
System.out.println("Setting bytes: " + Arrays.toString(bytes));
this.bytes = bytes;
}
public void setTrueOrFalse(Boolean trueOrFalse) {
System.out.println("Setting Boolean: " + trueOrFalse);
this.trueOrFalse = trueOrFalse;
}
public void setStringList(List<String> stringList) {
System.out.println("Setting string list with size: "
+ stringList.size());
this.stringList = stringList;
for (String string: stringList) {
System.out.println("String member: " + string);
}
}
public void setDate(Date date) {
System.out.println("Setting date: " + date);
this.date = date;
}
public void setFloatValue(Float floatValue) {
System.out.println("Setting float value: " + floatValue);
this.floatValue = floatValue;
}
public void setStream(InputStream stream) {
System.out.println("Setting stream: " + stream);
this.stream = stream;
}
public void setPattern(Pattern pattern) {
System.out.println("Setting pattern: " + pattern);
this.pattern = pattern;
}
public void setTrimString(String trimString) {
System.out.println("Setting trim string: " + trimString);
this.trimString = trimString;
}
public static class CustomPropertyEditorRegistrar implements PropertyEditorRegistrar {
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");
registry.registerCustomEditor(Date.class,
new CustomDateEditor(dateFormatter, true));
registry.registerCustomEditor(String.class, new StringTrimmerEditor(true));
}
}
public static void main(String... args) throws Exception {
File file = File.createTempFile("test", "txt");
file.deleteOnExit();
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:spring/app-context-01.xml");
ctx.refresh();
PropertyEditorBean bean =
(PropertyEditorBean) ctx.getBean("builtInSample");
ctx.close();
}
}
<?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:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="customEditorConfigurer"
class="org.springframework.beans.factory.config.CustomEditorConfigurer"
p:propertyEditorRegistrars-ref="propertyEditorRegistrarsList"/>
<util:list id="propertyEditorRegistrarsList">
<bean class="com.apress.prospring5.ch4.PropertyEditorBean$CustomPropertyEditorRegistrar"/>
</util:list>
<bean id="builtInSample" class="com.apress.prospring5.ch4.PropertyEditorBean"
p:character="A"
p:bytes="John Mayer"
p:cls="java.lang.String"
p:trueOrFalse="true"
p:stringList-ref="stringList"
p:stream="test.txt"
p:floatValue="123.45678"
p:date="05/03/13"
p:file="#{systemProperties['java.io.tmpdir']}#{systemProperties['file.separator']}test.txt"
p:locale="en_US"
p:pattern="a*b"
p:properties="name=Chris age=32"
p:trimString=" String need trimming "
p:url="https://spring.io/"
/>
<util:list id="stringList">
<value>String member 1</value>
<value>String member 2</value>
</util:list>
</beans>
执行结果
Setting bytes: [74, 111, 104, 110, 32, 77, 97, 121, 101, 114]
Setting character: A
Setting class: java.lang.String
Setting date: Wed May 03 00:00:00 CST 13
Setting file: test.txt
Setting float value: 123.45678
Setting locale: 英文 (美国)
Setting pattern: a*b
Loaded 1 properties
Setting stream: java.io.BufferedInputStream@d4342c2
Setting string list with size: 2
String member: String member 1
String member: String member 2
Setting trim string: String need trimming
Setting Boolean: true
Setting URL: https://spring.io/
4.12.2创建自定义PropertyEditor
方法
继承PropertyEditorSupport,覆盖setAsText(String)方法
package com.apress.prospring5.ch4.custom;
public class FullName {
private String firstName;
private String lastName;
public FullName(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String toString() {
return "First name: " + firstName + " - Last name: " + lastName;
}
}
package com.apress.prospring5.ch4.custom;
import java.beans.PropertyEditorSupport;
public class NamePropertyEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException {
String[] name = text.split("\\s");
setValue(new FullName(name[0], name[1]));
}
}
通过CustomEditorConfigurer注册所需编辑器
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="customEditorConfigurer"
class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="com.apress.prospring5.ch4.custom.FullName"
value="com.apress.prospring5.ch4.custom.NamePropertyEditor"/>
</map>
</property>
</bean>
<bean id="exampleBean" class="com.apress.prospring5.ch4.custom.CustomEditorExample"
p:name="John Mayer"/>
</beans>
package com.apress.prospring5.ch4.custom;
import org.springframework.context.support.GenericXmlApplicationContext;
public class CustomEditorExample {
private FullName name;
public FullName getName() {
return name;
}
public void setName(FullName name) {
this.name = name;
}
public static void main(String... args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:spring/app-context-02.xml");
ctx.refresh();
CustomEditorExample bean =
(CustomEditorExample) ctx.getBean("exampleBean");
System.out.println(bean.getName());
ctx.close();
}
}
执行结果
First name: John - Last name: Mayer
网友评论