java.beans.PropertyEditor与CustomEditorConfigurer

java.beans.PropertyEditor从名称看是属性编辑器,可用来将字符串值转换成指定类型的对象。有两个主要的方法:

void setValue(Object value);
void setAsText(String text);

一般不直接实现PropertyEditor接口,而是继承自PropertyEditor的一个实现类java.beans.PropertyEditorSupport简化工作,在子类覆盖setAsText方法是实现字符串到对象的转换工作,setValue方法一般不直接使用,在setAsText方法中将字符串进行转换并产生目标对象以后,由setAsText调用setValue来把目标对象注入到编辑器中。当然,你可用覆盖更多的方法来满足你的特殊要求。JavaBean的类和接口,被大部分spring包使用,可以从spring中学习更成熟的JavaBean使用方法。

示例分两部分,先演示PropertyEditor的简单实用,再演示Spring的org.springframework.beans.factory.config.CustomEditorConfigurer用法。

定义User.java类:

public class User {
private String name;
private int age;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

@Override
public String toString(){
return name+" : "+age;
}
}

User.java的属性编辑器类UserPropertyEditor.java:

import java.beans.PropertyEditorSupport;
public class UserPropertyEditor extends PropertyEditorSupport{

@Override
public void setAsText(String text){
String[] sts = text.split(",");
User user = new User();
user.setName(sts[0]);
user.setAge(Integer.parseInt(sts[1]));

setValue(user);
}
}

测试类:

public class PropertyEditorDemo {

public static void main(String[] args) {
UserPropertyEditor upe = new UserPropertyEditor();
upe.setAsText("Ellison,60");
//getValue()方法返回setAsText方法转换的User实例
System.out.println(upe.getValue());
}
}

结果输出:Ellison : 60
=====================
org.springframework.beans.factory.config.CustomEditorConfigurer可以读取实现PropertyEditor接口的类,并按其中的实现将字符串转换成指定类型对象。
新建一个类HelloBean.java

public class HelloBean {
private User user;

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}
}

Bean定义文件config.xml:

<?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="helloBean" class="test.HelloBean">
<property name="user" value="Gosling,59" />
</bean>

<bean id="editorConfig" class="org.springframework.beans.factory.config.CustomEditorConfigurer" >
<property name="customEditors">
<map>
<entry key="test.User">
<bean class="test.UserPropertyEditor"/>
</entry>
</map>
</property>
</bean>

</beans>

CustomEditorConfigurer的属性customEditors是Map类型,Map的key表示遇到哪一个类型时要使用PropertyEditor,value表示对应要使用的PropertyEditor实现类。

测试类:

public class PropertyEditorDemo {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("test/config.xml");
HelloBean hb = (HelloBean)ctx.getBean("helloBean");
System.out.println(hb.getUser());
}
}

结果输出:Gosling : 59
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值