色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

通過Setters方式對日期屬性及日期格式進行IOC注入

本實例中還涉及到Spring中采用多個配置文件,也涉及到對日期格式的注入-------更加靈活
Date屬性類: DatePropertyInjection.Java
復制代碼 代碼如下:
package com.zhmg.spring;
import Java.util.Date;
public class DatePropertyInjection {
private Date date;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}

屬性編輯器類:PropertyEditor.Java
復制代碼 代碼如下:
package com.zhmg.spring;
import Java.beans.PropertyEditorSupport;
import Java.text.ParseException;
import Java.text.SimpleDateFormat;
import Java.util.Date;
/**
* 自定義屬性編輯器處理Java.util.Date類型的屬性
* @author Administrator
*
*/
public class PropertyEditor extends PropertyEditorSupport {
String format = "yyyy-MM-dd";
public void setFormat(String format) {
this.format = format;
}
@Override
public void setAsText(String arg0) throws IllegalArgumentException {
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
try {
Date date = dateFormat.parse(arg0);
this.setValue(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

applicationContextBeans.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.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="dateProperty" class="com.zhmg.spring.DatePropertyInjection">
<property name="date">
<value>2009-8-28</value>
</property>
</bean>
</beans>

applicationContextBeans.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.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!--
<bean id="dateProperty" class="com.zhmg.spring.DatePropertyInjection">
<property name="date">
<value>2009-8-28</value>
</property>
</bean>
-->
<bean id="editor" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="Java.util.Date">
<bean class="com.zhmg.spring.PropertyEditor">
<!―對日期格式注入-->
<property name="format" value="yyyy-MM-dd"/>
</bean>
</entry>
</map>
</property>
</bean>
</beans>

測試單元:InjectionTest.Java
復制代碼 代碼如下:
package com.zhmg.spring;
import junit.framework.TestCase;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class InjectionTest extends TestCase {
BeanFactory factory;
protected void setUp() throws Exception {
//采用通配符的方式讀取所有以applicationContext開頭的配置文件
factory = new ClassPathXmlApplicationContext("applicationContext*.xml");
}
public void testInjection(){
DatePropertyInjection dateProp = (DatePropertyInjection)factory.getBean("dateProperty");
System.out.println("date=" + dateProp.getDate());
}
}

jsp技術通過Setters方式對日期屬性及日期格式進行IOC注入,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 米泉市| 阜平县| 天长市| 娄烦县| 大英县| 调兵山市| 东丰县| 宁河县| 化隆| 乌什县| 宾川县| 通化县| 镇宁| 株洲县| 武乡县| 蛟河市| 大关县| 廊坊市| 溆浦县| 呼图壁县| 玛曲县| 丰城市| 金山区| 汕尾市| 东兴市| 宁武县| 鄂伦春自治旗| 巴青县| 漳浦县| 墨江| 顺平县| 丹寨县| 普兰店市| 冕宁县| 旅游| 邹城市| 玉田县| 迁安市| 洞头县| 太谷县| 巢湖市|