Spring自定义类型注入

当函数的字段类型是int型等普通类型或集合类型时,Spring已经内置了属性编辑器,用来把配置文件中的字符串转换成相应的类型为相对应的类型注入值.同时,程序员也可以自己编写属性编辑器,来实现把字符型变量转换成日期型或自定义类型.
首先,设计一个培训机构类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集合类型.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值