Spring自定义属性编辑器

什么叫属性编辑器及其作用?

       *将spring配置文件中的字符串转换成相对应的java对象

       *spring内置了一些属性编辑器,也可以自定义属性编辑器

自定义属性编辑器的步骤:

  1. 继承PropertyEditorSupport
  2. 覆盖setAsText()方法
  3. 将自定义属性编辑器注入到spring中

实例代码:

UtilDatePropertyEditor.java

package utils;

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

public class UtilDatePropertyEditor extends PropertyEditorSupport{
	private String pattern;
	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		System.out.println("receive date :" + text);
		Date date = null;
		try {			
			date = new SimpleDateFormat(pattern).parse(text);				
			this.setValue(date);
		} catch (ParseException e) {			
			e.printStackTrace();
		}		
	}
	public String getPattern() {
		return pattern;
	}
	public void setPattern(String pattern) {
		this.pattern = pattern;
	}
	
}

applicationContext-editor.xml     将自定义的属性编辑器配置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:aop="http://www.springframework.org/schema/aop"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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">
			
	<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer"> 
		<property name="customEditors">
			<map>
				<entry key="java.util.Date">
					<!-- UtilDatePropertyEditor只在CustomEditorConfigurer文件中使用,不用指定id -->
					<bean class="utils.UtilDatePropertyEditor">
						<property name="pattern">
							<value>yyyy/MM/dd</value>
						</property>
					</bean>
				</entry>
			</map>
		</property>
	</bean>
</beans>

applicationContext-beans.xml   使用配置分离,更好的管理系统的配置文件

<?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:aop="http://www.springframework.org/schema/aop"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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">
	<bean id="bean1" class="cn.itcast.insection.Bean1">
		<property name="strValue" value="strValue"/>
		<property name="intValue" value="100"/>
		<property name="listValue">
			<list>
				<value>list1</value>
				<value>list2</value>
			</list>
		</property>
		<property name="arrayValue">
			<list>
				<value>array1</value>
				<value>array2</value>
			</list>
		</property>
		<property name="setValue">
			<list>
				<value>set1</value>
				<value>set2</value>
			</list>
		</property>
		<property name="mapValue">
			<map>
				<entry key="k1" value="v1"/>
				<entry key="k2" value="v2"/>				
			</map>
		</property>	
		<property name="dateValue">
			<value>2018/9/9</value>
		</property>					
	</bean>	
</beans>

Bean1.java

package cn.itcast.insection;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Bean1 {
	private String strValue;
	private int intValue;
	private List listValue;
	private Set setValue;
	private Map mapValue;
	private String[] arrayValue;
	private Date dateValue;
		
	public Date getDateValue() {
		return dateValue;
	}
	public void setDateValue(Date dateValue) {
		this.dateValue = dateValue;
	}
	public String getStrValue() {
		return strValue;
	}
	public void setStrValue(String strValue) {
		this.strValue = strValue;
	}
	public int getIntValue() {
		return intValue;
	}
	public void setIntValue(int intValue) {
		this.intValue = intValue;
	}
	public List getListValue() {
		return listValue;
	}
	public void setListValue(List listValue) {
		this.listValue = listValue;
	}
	public Set getSetValue() {
		return setValue;
	}
	public void setSetValue(Set setValue) {
		this.setValue = setValue;
	}
	public Map getMapValue() {
		return mapValue;
	}
	public void setMapValue(Map mapValue) {
		this.mapValue = mapValue;
	}
	public String[] getArrayValue() {
		return arrayValue;
	}
	public void setArrayValue(String[] arrayValue) {
		this.arrayValue = arrayValue;
	}	
}

InjectionTest.java 测试方法

package test;

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

import cn.itcast.insection.Bean1;
import junit.framework.TestCase;

public class InjectionTest extends TestCase {
	BeanFactory factory;
	@Override
	protected void setUp() throws Exception {
//		String[] configLocation = new String[]{"applicationContext-beans.xml","applicationContext-editor.xml"};
//		factory = new ClassPathXmlApplicationContext(configLocation);
		
		//通配符去加载配置文件
		factory = new ClassPathXmlApplicationContext("applicationContext-*.xml");		
	}

	@Override
	protected void tearDown() throws Exception {		
	}
	
	public void testInjection1(){
		Bean1 bean1 = (Bean1)factory.getBean("bean1");
		System.out.println("bean1.strValue=" + bean1.getStrValue());
		System.out.println("bean1.intValue=" + bean1.getIntValue());
		System.out.println("bean1.listValue=" + bean1.getListValue());
		System.out.println("bean1.setValue=" + bean1.getSetValue());
		System.out.println("bean1.mapValue=" + bean1.getMapValue());
		System.out.println("bean1.arrayValue=" + bean1.getArrayValue());
		System.out.println("bean1.dateValue=" + bean1.getDateValue());			
	}
}

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值