导入测试jar包junit-4.8.2.jar
字符串自定义Date转化
1.添加分拆配置文件applicationContext_editor.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- id是唯一性标识 -->
<bean id="customEditors" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date" value ="com.xiaoma.demo.UtilDatePropertyEditor" />
</map>
</property>
</bean>
</beans>
第一个配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- id是唯一性标识 -->
<bean id="bean1" class="com.xiaoma.demo.Bean1 ">
<property name="name" value="xiaoliu" />
<property name="dateValue" value="2018-1-15"></property>
</bean>
</beans>
创建实体Bean1
package com.xiaoma.demo;
import java.awt.List;
import java.util.Date;
import java.util.Map;
import java.util.Set;
public class Bean1 {
private String name;
private int age;
private List lists;
private Set set;
private String [] arrys;
private Map maps;
private Date dateValue;
public Date getDateValue() {
return dateValue;
}
public void setDateValue(Date dateValue) {
this.dateValue = dateValue;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List getLists() {
return lists;
}
public void setLists(List lists) {
this.lists = lists;
}
public Set getSet() {
return set;
}
public void setSet(Set set) {
this.set = set;
}
public String[] getArrys() {
return arrys;
}
public void setArrys(String[] arrys) {
this.arrys = arrys;
}
public Map getMaps() {
return maps;
}
public void setMaps(Map maps) {
this.maps = maps;
}
}
创建自定义属性编辑器 UtilDatePropertyEditor
package com.xiaoma.demo;
import java.beans.PropertyEditorSupport;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* AOP声明式服务能力
* 对于资源,Hibernate Session 或JDBC Connection 我们不负责开启和关闭
* 面向接口编程
* 减少代码中的耦合,将耦合配置到配置文件之中
*
* @author Sugar
* 时间转换器,将字符串类型转化为java.util.Date
*
*/
public class UtilDatePropertyEditor extends PropertyEditorSupport{
@Override
public void setAsText(String text) throws IllegalArgumentException {
System.out.println(text);
try {
Date parse = new SimpleDateFormat("yyyy-MM-dd").parse(text);
this.setValue(parse);
} catch (Exception e) {
// TODO: handle exception
}
}
}
运用测试类ShowTest
package test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xiaoma.demo.Bean1;
import junit.framework.TestCase;
public class ShowTest extends TestCase{
private BeanFactory factory;
@Override
protected void setUp() throws Exception {
//多配置文件的加载
String [] configLocations = new String []{"applicationContext.xml","applicationContext_editor.xml"};
factory = new ClassPathXmlApplicationContext(configLocations);
}
public void testShow(){
Bean1 bean1 = (Bean1) factory.getBean("bean1");
String name = bean1.getName();
System.out.println("name:"+name);
}
@Override
protected void tearDown() throws Exception {
// TODO Auto-generated method stub
super.tearDown();
}
}
PS:使用spring4.0报错问题
<!-- id是唯一性标识 -->
<bean id="customEditors"
class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class="com.xiaoma.demo.UtilDatePropertyEditor"></bean>
</entry>
</map>
</property>
</bean>
报错Failed to convert property value of type ‘java.util.LinkedHashMap’ to required type ‘java.util.Map’ for property ‘customEditors’;
是由于2.0api与4.0api的区别,在4.0之中改为如下
<!-- id是唯一性标识 -->
<bean id="customEditors" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date" value ="com.xiaoma.demo.UtilDatePropertyEditor" />
</map>
</property>
</bean>
报错问题解决