spring中的属性编辑器

来源:互联网 发布:qq群淘宝领优惠券骗局 编辑:程序博客网 时间:2024/05/29 16:29

spring中的属性编辑器:
1.属性编辑器本身是由Java提供了,Spring只是利用了这一特性使装配一个Bean更容易而已。
2.应用场景:
如果在有一个pojo类Demo.java,其只有一个java.util.Date类型的属性,代码如下:
public class Demo {

 public Date date;

 public Date getDate() {
  return date;
 }
 public void setDate(Date date) {
  this.date = date;
 }
}

其在spring的配置文件applicationContext.xml中的配置为:
<bean id="demo" class="org.huang.Demo" abstract="false"
  lazy-init="default" autowire="default" dependency-check="default">
  <property name="date">
   <value>2008-08-01</value>
  </property>
 </bean>

客户端的代码为:
        BeanFactory factory=new ClassPathXmlApplicationContext  ("applicationContext.xml"); 
 Demo demo=(Demo) factory.getBean("demo");
 System.out.println(demo.getDate().toString());

如果执行如上代码,则控制台会抛出异常,异常信息大概如下:
org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property 'date'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'date': no matching editors or conversion strategy found

这表示spring无法找到合适的转换策略,需要自己写一个转换器,在spring中称之为属性编辑器。spring中的属性编辑器可以将字符串转换为相应的对象,然后注入到其它对象中去。编写自己的属性编辑器的步骤很简单,属性编辑器类需要从java.beans.PropertyEditorSupport类继承,在这个类中有一个setAsText方法,这个方法有一个String类型的参数,通过这个方法,可以将String类型的参数值转换成其他类型的属性。在这个方法中我们还需要使用一个setValue方法,就来指定转换后的对象实例。
下面我们就来编写这个实例的属性编辑器:其代码如下:
public class DatePropertyEditor extends PropertyEditorSupport {

 public void setAsText(String text) throws IllegalArgumentException {
  
  SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
  try {
   this.setValue(format.parse(text));
  } catch (ParseException e) {
   e.printStackTrace();
  }
  super.setAsText(text);
 }
}

属性编辑器写完以后要将这个属性编辑器配置到spring配置文件中去,通过注册让spring知道:
其配置如下:
<bean id="customEditorConfigurer"  class="org.springframework.beans.factory.config.CustomEditorConfigurer">
  <property name="customEditors">
     <map>
       <entry key="java.util.Date" >
         <bean class="com.huang.DatePropertyEditor"></bean>
       </entry>
     </map>
  </property>
 </bean>
CustomEditorConfigurer实现BeanFactoryPostProcessor接口,因此是一个Bean工厂后处理器。我们知道Bean工厂后处理器在Spring容器加载配置文件并生成BeanDefinition半成品后就会被自动执行。因此CustomEditorConfigurer有容器启动时有机会注入自定义的属性编辑器。
属性编辑器是使用了CustomEditorConfigurer的customEditors属性进行安装的。这是一个Map类型的属性。key的值必须是属性编辑器最终转换后的类型名,如在本例中,要将文本的参数值转换成java.util.Date,因此,key必须为java.util.Date。
这样就完成了属性编辑器的注册,然后运行客户端的代码,控制台将打印Fri Aug 01 00:00:00 CST 2008


PropertyEditor
PropertyEditor是属性编辑器的接口,它规定了将外部设置值转换为内部JavaBean属性值的转换接口方法。PropertyEditor主要的接口方法说明如下:

—  Object getValue():返回属性的当前值。基本类型被封装成对应的封装类实例;

—  void setValue(Object newValue):设置属性的值,基本类型以封装类传入;

—  String getAsText():将属性对象用一个字符串表示,以便外部的属性编辑器能以可视化的方式显示。缺省返回null,表示该属性不能以字符串表示;

—  void setAsText(String text):用一个字符串去更新属性的内部值,这个字符串一般从外部属性编辑器传入;

—  String[] getTags():返回表示有效属性值的字符串数组(如boolean属性对应的有效Tag为true和false),以便属性编辑器能以下拉框的方式显示出来。缺省返回null,表示属性没有匹配的字符值有限集合;

—  String getJavaInitializationString():为属性提供一个表示初始值的字符串,属性编辑器以此值作为属性的默认值。

可以看出PropertyEditor接口方法是内部属性值和外部设置值的沟通桥梁。此外,我们可以很容易地发现该接口的很多方法是专为IDE中的可视化属性编辑器提供的:如getTags()、getJavaInitializationString()以及另外一些我们未此介绍的接口方法。

Java为PropertyEditor提供了一个方便类:PropertyEditorSupport,该类实现了PropertyEditor接口并提供默认实现,一般情况下,用户可以通过扩展这个方便类设计自己的属性编辑器。

 

原创粉丝点击