(注:这个曾发表在某Blog上,那个马上要没了,你懂的=。=)
SSH相信很多整过J2EE的人都很熟悉,但可能很多人没有深入研究过其底层架构,我觉得其中spring的源码是记得详细研究的,如果能对其IOC,AOP的整个流程有清晰的了解,就能对其进行扩展来满足自己项目的需求,在遇到bug也能更好的解决问题。
下面分析两个spring中比较优雅的功能的底层实现来看看spring的扩展思路。
1. spring自定义属性编辑器
(1)使用
要使用spring的自定义属性编辑器,只需要让自己的属性编辑器继承java.beans.PropertyEditorSupport类,并重写其中setAsText(String text),将text装换成目标类型后,调用setValue设置目标值。
实例:
Java代码:
自定义属性编辑器:
public class CustomDatePropertyEditor extends java.beans.PropertyEditorSupport{
private String format;
public String getFormat()
{
return format;
}
public void setFormat(String format) {
this.format = format;
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
SimpleDateFormat f = new SimpleDateFormat(format);
try {
this.setValue(f.parse(text));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Spring配置文件:
<bean id="userinfo" class="cn.hibernate.UserInfo">
<property name="name">
<value>mr</value>
</property>
<property name="sign">
<value>2011-11-28</value>