概述
Spring自带了7种PropertyEditor实现,分别是
ByteArrayPropertyEditor 对应 byte[]属性
ClassEditor 对应Class属性
FileEditor对应File属性
LocaleEditor对应Locale属性
PropertiesEditor对应Properties属性
StringArrayPropertyEditor对应String[]属性
URLEditor对应URL属性
也就是说,我们以字符串的形式给上述类型的变量注入时候,spring会帮我们自动进行类型转换,其中要注意的是ClassEditor要求注入的类名字符串不能有任何多余的空格,否则会有ClassCastException
同时,我们也可以自定义PropertyEditor,本文以一个正则搜索为例
PatternPropertyEditor.java
import java.beans.PropertyEditorSupport;
import java.util.regex.Pattern;
public class PatternPropertyEditor extends PropertyEditorSupport ... {
public void setAsText(String text) throws IllegalArgumentException ...{
Pattern pattern=Pattern.compile(text);
setValue(pattern); //参数是bean的属性类型对象
}
}
ExampleBean.java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ExampleBean ... {
private Pattern searchPattern; //注入的正则字符串转换成Pattern对象,pattern的编译在CustomerPropertyEditor中完成
private String textToSearch;
//返回查找结果数量
public int getMaxCount()...{
Matcher matcher=searchPattern.matcher(textToSearch);
int count=0;
while(matcher.find())...{
count++;
}
return count;
}
public Pattern getSearchPattern() ...{
return searchPattern;
}
public void setSearchPattern(Pattern searchPattern) ...{
this.searchPattern = searchPattern;
}
public String getTextToSearch() ...{
return textToSearch;
}
public void setTextToSearch(String textToSearch) ...{
this.textToSearch = textToSearch;
}
}
配置文件:
< beans
xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >
< bean id ="customEditorConfigure" class ="org.springframework.beans.factory.config.CustomEditorConfigurer" >
< property name ="customEditors" >
< map >
< entry key ="java.util.regex.Pattern" > <!-- key为注入对应的类型,和ExampleBean中的searchPattern类型保持一致 -->
< bean class ="ch5.propertyEditor.PatternPropertyEditor" ></ bean >
</ entry >
</ map >
</ property >
</ bean >
< bean id ="exampleBean" class ="ch5.propertyEditor.ExampleBean" >
< property name ="searchPattern" >
< value > (dog|fish) </ value >
</ property >
< property name ="textToSearch" >
< value > this is dog and dog and fish and fish and fish </ value >
</ property >
</ bean >
</ beans >
测试代码:
import java.io.File;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.CustomEditorConfigurer;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class TestSpring ... {
public static void main(String args[]) throws Exception...{
//获取bean factory
ConfigurableListableBeanFactory factory=(ConfigurableListableBeanFactory)getBeanFactory(); //使用子beanFactory
//配置自动以propertyEditor
CustomEditorConfigurer config=(CustomEditorConfigurer)factory.getBean("customEditorConfigure");
config.postProcessBeanFactory(factory);
ExampleBean example=(ExampleBean)factory.getBean("exampleBean");
System.out.println(example.getMaxCount());
}
public static BeanFactory getBeanFactory()...{
//获取bean factory
String realpath="";
//加载配置项
realpath=System.getProperty("user.dir")+File.separator+"src"+File.separator+"ch5/propertyEditor"+File.separator+"applicationContext.xml";
ConfigurableListableBeanFactory factory=new XmlBeanFactory(new FileSystemResource(realpath));
return factory;
}
}
运行结果:
5
加载PropertyEditor的方法还有一种,就是调用ConfigurableBeanFactory.registerCustomerEditor方法(参数分别是实用的具体类型合编辑器实例,也就是本文的Pattern和PatternPropertyEditor),但并不推荐这样使用,因为,每当我们定义一个新的CustomerPropertyEditor时候,都需要修改代码,远不如声明式的简单明了
本文的方法需要注意的是,只能为Pattern注册一个PropertyEditor
最后
以上就是外向音响为你收集整理的《Pro Spring》学习笔记之PropertyEditor的全部内容,希望文章能够帮你解决《Pro Spring》学习笔记之PropertyEditor所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复