`
imaginecup
  • 浏览: 85585 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

Spring属性编辑器

阅读更多


什么是属性编辑器,作用?
 * 自定义属性编辑器,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());
		
	}

}

 

1
0
分享到:
评论

相关推荐

    spring中的自定义属性编辑器

    spring中的自定义属性编辑器,是我博客中的源代码,spring中的自定义属性编辑器,是我博客中的源代码

    spring 自定义属性编辑器

    NULL 博文链接:https://wooden-baby.iteye.com/blog/506838

    spring2.0(三) 自定义属性编辑器

    NULL 博文链接:https://yxwang0615.iteye.com/blog/969360

    Spring学习笔记(11)----自定义属性编辑器

    NULL 博文链接:https://coolszy.iteye.com/blog/522095

    自定义属性编辑及Spring处理器映射

    java中的属性编辑器详细说明及Spring中AnnotationMethodHandlerAdapter说明和DefaultAnnotationHandlerMapping的说明及用法

    浅谈Spring的属性编辑器的使用

    主要介绍了浅谈Spring的属性编辑器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    springmvc自定义属性编辑器和参数解析器

    springmvc自定义属性编辑器和参数解析器

    Springframework开发参考手册chm格式

     Spring Framework 开发手册 chm,一份对Spring特性的参考指南,内容涵盖Spring概述、使用场景、Spring2.0新特性、面向切面编程、中间层、WEB层、校验,数据绑定,BeanWrapper,与属性编辑器、使用Spring进行面向...

    yml编辑器eclipse插件(yamleditor)

    springboot流行用yml属性文件,由于其特殊的语法,没有编辑器可能容易写错,而察觉不到,导致程序出了问题都不知道问题出在哪里。 yaml Editor是eclipse应用商店推荐的一款yml编辑器,为避免在线安装过慢,特分享...

    spring结合Mina

    spring结合Mina的配置文件,设计到spring构造 属性编辑器

    spring数据格式转换

    spring数据格式转换 自定义的属性编辑器进行数据绑定

    spring.net中文手册在线版

    4.3.3.详细讨论对象属性和构造器参数 4.3.3.1.设置空值 4.3.3.2.设置集合值 4.3.3.3.设置泛型集合的值 4.3.3.4.设置索引器属性 4.3.3.5.内联对象定义 4.3.3.6.idref节点 4.3.3.7.引用协作对象 4.3.3.8.value和ref...

    Spring 2.0 开发参考手册

    5. 校验,数据绑定,BeanWrapper,与属性编辑器 5.1. 简介 5.2. 使用Spring的Validator接口进行校验 5.3. 从错误代码到错误信息 5.4. Bean处理和BeanWrapper 5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建...

    Spring-Reference_zh_CN(Spring中文参考手册)

    5. 校验,数据绑定,BeanWrapper,与属性编辑器 5.1. 简介 5.2. 使用Spring的Validator接口进行校验 5.3. 从错误代码到错误信息 5.4. Bean处理和BeanWrapper 5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建的...

    Spring中文帮助文档

    5. 校验,数据绑定,BeanWrapper,与属性编辑器 5.1. 简介 5.2. 使用Spring的Validator接口进行校验 5.3. 从错误代码到错误信息 5.4. Bean处理和BeanWrapper 5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建...

    SpringPropertyEditorDemo:演示使用自定义属性编辑器绑定Spring-MVC中用户定义的对象的集合

    SpringPropertyEditorDemo 演示使用自定义属性编辑器绑定Spring-MVC中用户定义的对象的集合

    Spring API

    5. 校验,数据绑定,BeanWrapper,与属性编辑器 5.1. 简介 5.2. 使用Spring的Validator接口进行校验 5.3. 从错误代码到错误信息 5.4. Bean处理和BeanWrapper 5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建...

    spring chm文档

    5. 校验,数据绑定,BeanWrapper,与属性编辑器 5.1. 简介 5.2. 使用Spring的Validator接口进行校验 5.3. 从错误代码到错误信息 5.4. Bean处理和BeanWrapper 5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建...

    Spring攻略(第二版 中文高清版).part1

    2.15 在Spring中注册属性编辑器 96 2.15.1 问题 96 2.15.2 解决方案 96 2.15.3 工作原理 97 2.16 创建自定义属性编辑器 99 2.16.1 问题 99 2.16.2 解决方案 100 2.16.3 工作原理 100 2.17 使用...

    Spring.3.x企业应用开发实战(完整版).part2

    5.2.2 Spring默认属性编辑器 5.2.3 自定义属性编辑器 5.3 使用外部属性文件 5.3.1 使用外部属性文件 5.3.2 使用加密的属性文件 5.3.3 属性文件自身的引用 5.4 引用Bean的属性值 5.5 国际化信息 5.5.1 基础知识 5.5.2...

Global site tag (gtag.js) - Google Analytics