当函数的字段类型是int型等普通类型或集合类型时,Spring已经内置了属性编辑器,用来把配置文件中的字符串转换成相应的类型为相对应的类型注入值.同时,程序员也可以自己编写属性编辑器,来实现把字符型变量转换成日期型或自定义类型.
首先,设计一个培训机构类AttributeEditor,代码如下:
接着,设计一个测试类UtilDatePropertyEditor,代码如下:
Junit测试代码如下:
其中,Spring配置文件applicationContext.xml配置如下:
说明:属性编辑器类UtilDatePropertyEditor中,首先要继承PropertyEditorSupport,
该类在包java.bean.PropertyEditorSupport中.接着要覆盖类PropertyEditorSupport中的setAsText方法,该方法中就是实现类型转换的具体代码。因为setAsText没有返回值,所以把SImpleDateFormat的返回值传入到PopertyEditorSupport方法的setValue方法中.
编写完属性编辑器后,就要把该属性编辑器注入到IOC容器中,如下面代码:
首先要把自己编写的属性编辑器注入到org.springframework.beans.factory.config.CustomEditorConfigurer中,查看API文档,可以得到如下定义:
从上面两段代码可以看出,属性编辑器被注入到CustomEditorConfigurer方法的customEditors中,同时要注意customEditors是Map集合类型.
首先,设计一个培训机构类AttributeEditor,代码如下:
import java.util.Date
public class AttributeEditor {
private Date dateValue;
public Date getDateValue() {
return dateValue;
}
public void setDateValue(Date dateValue) {
this.dateValue = dateValue;
}
}
接着,设计一个测试类UtilDatePropertyEditor,代码如下:
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class UitlDatePropertyEditor extends PropertyEditorSupport {
private String format = "yyyy-MM-dd"; //定义固定字符串格式
//重写父类方法
public void setAsText(String text)
throws IllegalArgumentException{
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
Date date = sdf.parse(text); //获取时间信息
this.setValue(date);
} catch(ParseException) {
e.printStackTrace();
}
}
public void setFormat(String format) {
this.format = format;
}
}
Junit测试代码如下:
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import junit.framework.TestCase;
public class TestAttrEditor extends TestCase {
private BeanFactory factory;
//重写父类方法
protected void setup() throws Exception {
factory = new ClassPathXmlApplicationContext ("applicationContext.xml");
}
public void testInjection() {
AttributeEditor ae = (AttributeEditor)factory.getBean("attributEditor");
System.out.println("bean.dateValue = " + ae.getDateValue());
}
}
其中,Spring配置文件applicationContext.xml配置如下:
<bean id="customEditorConfigurer"
class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class=".../UtilDatePropertyEditor">
<!--设置时间格式-->
<property name="format" value="yyyy-MM-dd">
</bean>
</entry>
</map>
</property>
</bean>
<!--创建对象attributeEditor-->
<bean id="attributeEditor" class=".../AttributeEditor">
<property name="dateValue">
<value>2009-12-01</value>
</property>
</bean>
说明:属性编辑器类UtilDatePropertyEditor中,首先要继承PropertyEditorSupport,
该类在包java.bean.PropertyEditorSupport中.接着要覆盖类PropertyEditorSupport中的setAsText方法,该方法中就是实现类型转换的具体代码。因为setAsText没有返回值,所以把SImpleDateFormat的返回值传入到PopertyEditorSupport方法的setValue方法中.
编写完属性编辑器后,就要把该属性编辑器注入到IOC容器中,如下面代码:
<bean id="customEditorConfigurer"
class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class=".../UtilDatePropertyEditor">
<!--设置时间格式-->
<property name="format" value="yyyy-MM-dd">
</bean>
</entry>
</map>
</property>
</bean>
首先要把自己编写的属性编辑器注入到org.springframework.beans.factory.config.CustomEditorConfigurer中,查看API文档,可以得到如下定义:
public class CustomEditorConfigurer() {
private Map customEditors;
public void setCustomEditors(Map customEditors) {
this.customEditors = customEditors;
}
public Map getCustomEditors() {
return customEditors;
}
}
从上面两段代码可以看出,属性编辑器被注入到CustomEditorConfigurer方法的customEditors中,同时要注意customEditors是Map集合类型.