现在的位置: 首页 > 综合 > 正文

spring_Date属性编辑器和junit测试.

2013年12月12日 ⁄ 综合 ⁄ 共 4424字 ⁄ 字号 评论关闭

首先在项目中加入对spring的支持,也就是把spring相关的jar加入到项目中,还有junit的jar包

 

package util;

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

public class utilDatePropertyEditor extends PropertyEditorSupport {
 
 private String format;//由spring来注入格式
 @Override
 public void setAsText(String text) throws IllegalArgumentException {
  // TODO Auto-generated method stub
  //super.setAsText(arg0);
  SimpleDateFormat sdf = new SimpleDateFormat(format);
  try {
   Date date = sdf.parse(text);
   this.setValue(date);
  } catch (ParseException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 public String getFormat() {
  return format;
 }
 public void setFormat(String format) {
  this.format = format;
 }

}
========================关于date格式的applicationcontext.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"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
 
 <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer" >
  <property name="customEditors">
   <map>
    <entry key="java.util.Date">

<!-- 内部Bean-->
     <bean class="util.utilDatePropertyEditor">
      <property name="format">
      <value>yyyy-MM-dd</value>
      </property>
     </bean>
    </entry>
   </map>
  </property>
 </bean>
</beans>

=================================关于各种属性的赋值=============================

<?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-2.0.xsd">
 
 <bean id="bean1" class="testBean.Bean1" >
 <property name="str">
  <value>aaa</value>
 </property>
 <property name="intvalue">
  <value>123</value>
 </property>
 <property name="strsvalue">
  <list>
   <value>123</value>
   <value>3232</value>
  </list>
 </property>
 <property name="listvalue">
  <list>
   <value>1123232323</value>
   <value>3232</value>
  </list>
 </property>
 <property name="setvalue">
  <set>
   <value>222</value>
   <value>22222</value>
  </set>
 </property>
 <property name="mapvalue">
  <map>
   <entry key="key1" value="v1"></entry>
   <entry key="key2" value="v2"></entry>
  </map>
 </property>
 <property name="date" value="2009-09-09"></property><!-- 和日期属性编辑器配合使用,这样在赋值date型数据的时候就不会出错了 -->
 </bean>
</beans>

================================junit test class======================================

package test;

import junit.framework.TestCase;

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

import testBean.Bean1;

public class test extends TestCase {
 
 private BeanFactory beanFactory;
 public void test(){
  Bean1 bean = (Bean1)beanFactory.getBean("bean1");
  System.out.println(bean.getIntvalue());
  System.out.println(bean.getStr());
  System.out.println(bean.getListvalue());
  System.out.println(bean.getMapvalue());
  System.out.println(bean.getSetvalue());
  System.out.println(bean.getStrsvalue());
  System.out.println(bean.getDate());
 }

 @Override
 protected void setUp() throws Exception {
  // TODO Auto-generated method stub
  beanFactory = new ClassPathXmlApplicationContext("applicationContext-*.xml");//初始化配置文件,以后的方法就不用new了

 }
}

==============================bean1=============================

package testBean;

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

@SuppressWarnings("unchecked")
public class Bean1 {
 
 private String str;
 
 private int intvalue;
 
 
 private List listvalue;
 
 private Set setvalue;
 
 private Map mapvalue;
 
 private String[] strsvalue;
 
 private Date date;

 public Date getDate() {
  return date;
 }

 public void setDate(Date date) {
  this.date = date;
 }

 public String getStr() {
  return str;
 }

 public void setStr(String str) {
  this.str = str;
 }

 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[] getStrsvalue() {
  return strsvalue;
 }

 public void setStrsvalue(String[] strsvalue) {
  this.strsvalue = strsvalue;
 }
 
}

抱歉!评论已关闭.