国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

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

2024-09-05 00:21:05
字體:
來源:轉載
供稿:網友
Spring不直接支持對Date類型的IOC依賴關系的注入,而提供了類似于Struts中的converte一樣的屬性編輯器,需要程序員自己寫相應的類,并將其注冊。

本實例中還涉及到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/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean>
<property>
<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/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!--
<bean>
<property>
<value>2009-8-28</value>
</property>
</bean>
-->
<bean>
<property>
<map>
<entry key="java.util.Date">
<bean>
<!—對日期格式注入-->
<property 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());
}
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 岐山县| 泸州市| 招远市| 南昌市| 磴口县| 麦盖提县| 郁南县| 浦东新区| 耿马| 河津市| 汝阳县| 旺苍县| 龙门县| 普安县| 昌图县| 通州市| 郯城县| 博爱县| 阜阳市| 德州市| 河津市| 缙云县| 静宁县| 台湾省| 鹤岗市| 安乡县| 璧山县| 崇左市| 闵行区| 高淳县| 慈溪市| 隆林| 镇康县| 平武县| 友谊县| 措勤县| 东光县| 伊川县| 昌乐县| 滕州市| 萍乡市|