属性编辑器:
自定义的属性编辑器是将spring 配置文件中的字符串转换成相应的对象进行注入。spring 本身自定义了很多编辑器,我们也可以自己编写自己的编辑器。
1.编写编辑器 继承 PropertyEditorSupport
import java.beans.PropertyEditorSupport;
import java.text.SimpleDateFormat;
import java.util.Date;
public class UtilDateProertyEditor extends PropertyEditorSupport {
private String format = "yyyy-MM-dd";
public void setAsText(String text)throws IllegalArgumentException{
SimpleDateFormat sdf = new SimpleDateFormat(format);
try{
Date utilDate = sdf.parse(text);
this.setValue(utilDate);
}catch(Exception e){e.printStackTrace();}
}
}
2.将编写好的编辑器注入到applicationContext.xml文件中 ,也可以单独写一个xml文件,对该部分内容进行配置
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class="com.bjsxt.spring.UtilDateProertyEditor"/>
</entry>
</map>
</property>
</bean>
自定义的属性编辑器是将spring 配置文件中的字符串转换成相应的对象进行注入。spring 本身自定义了很多编辑器,我们也可以自己编写自己的编辑器。
1.编写编辑器 继承 PropertyEditorSupport
import java.beans.PropertyEditorSupport;
import java.text.SimpleDateFormat;
import java.util.Date;
public class UtilDateProertyEditor extends PropertyEditorSupport {
private String format = "yyyy-MM-dd";
public void setAsText(String text)throws IllegalArgumentException{
SimpleDateFormat sdf = new SimpleDateFormat(format);
try{
Date utilDate = sdf.parse(text);
this.setValue(utilDate);
}catch(Exception e){e.printStackTrace();}
}
}
2.将编写好的编辑器注入到applicationContext.xml文件中 ,也可以单独写一个xml文件,对该部分内容进行配置
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class="com.bjsxt.spring.UtilDateProertyEditor"/>
</entry>
</map>
</property>
</bean>