《Pro Spring》学习笔记之PropertyEditor

本文介绍如何在Spring中自定义PropertyEditor实现特定类型的转换,通过示例演示将正则表达式字符串转换为Pattern对象的过程,并展示配置及测试方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring自带了7种PropertyEditor实现,分别是

ByteArrayPropertyEditor 对应 byte[]属性
ClassEditor 对应Class属性
FileEditor对应File属性
LocaleEditor对应Locale属性
PropertiesEditor对应Properties属性
StringArrayPropertyEditor对应String[]属性
URLEditor对应URL属性

也就是说,我们以字符串的形式给上述类型的变量注入时候,spring会帮我们自动进行类型转换,其中要注意的是ClassEditor要求注入的类名字符串不能有任何多余的空格,否则会有ClassCastException

同时,我们也可以自定义PropertyEditor,本文以一个正则搜索为例

PatternPropertyEditor.java

 

package ch5.propertyEditor;

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

 

package ch5.propertyEditor;
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;
}

}

 

配置文件:

 

<?xml version="1.0" encoding="UTF-8"?>
<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>

 

测试代码:

 

package ch5.propertyEditor;

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值