Spring属性编辑器

本文介绍Spring框架中的属性编辑器概念及使用方法,包括自定义属性编辑器的步骤,并通过一个日期处理的例子展示了如何在配置文件和程序中注册自定义属性编辑器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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

 * 如何定义属性编辑器?
  * 继承PropertyEditorSupport类,覆写setAsText()方法(注意要将处理完成的对象通过PropertyEditorSupport的setValue设置回去)

 *向IoC容器中注册自定义的属性编辑器(两种方式:1 在配置文件中注册 2 在程序中注册)

接下来我们就看看一个关于日期的属性编辑器的部署过程:

首先定义一个测试类:

package com.property;

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;
	}

}

 然后继承PropertyEditorSupport,实现自定义的属性编辑器:

package com.property;
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(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;
 }
 
}

 接着我们首先定义配置文件:(采用配置方式)

<?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans.xsd
">
<bean id="user" class="com.property.User">
	<property name="username" value="imaginecup"/>
	<property name="birthday" value="1989-05-12"/>
</bean>
<!-- 自定义的属性编辑器 -->
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
		<property name="customEditors">
			<map>
				<entry key="java.util.Date">
					<bean class="com.property.UtilDatePropertyEditor">
						<property name="format" value="yyyy-MM-dd"/>
					</bean>
				</entry>
			</map>
		</property>
	</bean>
</beans>

 

package com.property;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.CustomEditorConfigurer;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

public class TestPropertyEditor {
	public static void main(String[] args) {
//		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext-*.xml");
//		User user=(User)ac.getBean("user");
//		System.out.println("name:"+user.getUsername()+"\nbirthday:"+user.getBirthday());
		//它等价于
		ConfigurableBeanFactory beanFactory=new XmlBeanFactory(new ClassPathResource("applicationContext-beans.xml"));
		CustomEditorConfigurer configurer=(CustomEditorConfigurer)beanFactory.getBean("customEditorConfigurer");
		configurer.postProcessBeanFactory((ConfigurableListableBeanFactory) beanFactory);
		User user=(User)beanFactory.getBean("user");
		System.out.println("name:"+user.getUsername()+"\nbirthday:"+user.getBirthday());
		
	}

}

 

输出结果:

name:imaginecup
birthday:Fri May 12 00:00:00 CDT 1989

 

接下来我们在程序中注册自定义的属性编辑器:

package com.property;

import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class TestPropertyEditorResgister {
	public static void main(String[] args) {
		ConfigurableBeanFactory beanFactory=new XmlBeanFactory(new ClassPathResource("applicationContext-beans.xml"));
		beanFactory.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
			public void registerCustomEditors(PropertyEditorRegistry registry) {
				registry.registerCustomEditor(java.util.Date.class, new UtilDatePropertyEditor("yyyy-MM-dd"));
				
			}
		});
		User user=(User)beanFactory.getBean("user");
		System.out.println("name:"+user.getUsername()+"\nbirthday:"+user.getBirthday());
		
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值