spring中自定义属性编辑器CustomEditorConfigurer

什么是属性编辑器,作用?
* 自定义属性编辑器,spring配置文件中的字符串转换成相应的对象进行注入
spring已经有内置的属性编辑器,我们可以根据需求自己定义属性编辑器

* 如何定义属性编辑器?
* 继承PropertyEditorSupport类,覆写setAsText()方法,参见:UtilDatePropertyEditor.java

* 将属性编辑器注册到spring中,参见:applicationContext.xml ;

直接上代码。

实体类:

package com.spring.propertyEditor;

import java.util.Date;

public class User {
	private String username;
	private Date birthday;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

}

自定义属性编辑器:UtilDatePropertyEditor.java 如下,必须继承java.beans.PropertyEditorSupport类,覆写setAsText()方法 。

package com.spring.propertyEditor;

import java.beans.PropertyEditorSupport;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class UtilDatePropertyEditor extends PropertyEditorSupport {
	private String format;

	
	public UtilDatePropertyEditor() {
	}

	public UtilDatePropertyEditor(String format) {
		this.format = format;
	}

	public void setAsText(String text) throws IllegalArgumentException {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			Date date = sdf.parse(text);
			this.setValue(date);
		} catch (ParseException e) {
			System.out.println(e.getMessage() + "日期的格式不对");
		}
	}

	public void setFormat(String format) {
		this.format = format;
	}

}

spring配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd" >
	<bean id="user" class="com.spring.propertyEditor.User">
		<property name="username">
			<value>test</value>
		</property>
		<property name="birthday">
			<value>2008-08-15</value>
		</property>
	</bean>
	<!-- 于是定义属性编辑器 -->
	<bean id="customEditorConfigurer"
		class="org.springframework.beans.factory.config.CustomEditorConfigurer">
		<property name="customEditors">
			<map>
				<entry key="java.util.Date">
					<bean class="com.spring.propertyEditor.UtilDatePropertyEditor">
						<!--干脆把format也注入,灵活处理格式 -->
						<property name="format">
							<value>yyyy-MM-dd</value>
						</property>
					</bean>
				</entry>
			</map>
		</property>
	</bean>
</beans>

测试类:

package com.spring.propertyEditor;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestPropertyEditor {
	public static void main(String[] args) {
		BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring.xml");   
		User user = (User) beanFactory.getBean("user");
		System.out.println("name:" + user.getUsername() + "\nbirthday:"
				+ user.getBirthday());

	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值