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

通過(guò)Setters方式對(duì)日期屬性及日期格式進(jìn)行IOC注入

本實(shí)例中還涉及到Spring中采用多個(gè)配置文件,也涉及到對(duì)日期格式的注入-------更加靈活
Date屬性類: DatePropertyInjection.Java
復(fù)制代碼 代碼如下:
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
復(fù)制代碼 代碼如下:
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
復(fù)制代碼 代碼如下:
<?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
復(fù)制代碼 代碼如下:
<?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">
<!―對(duì)日期格式注入-->
<property name="format" value="yyyy-MM-dd"/>
</bean>
</entry>
</map>
</property>
</bean>
</beans>

測(cè)試單元:InjectionTest.Java
復(fù)制代碼 代碼如下:
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技術(shù)通過(guò)Setters方式對(duì)日期屬性及日期格式進(jìn)行IOC注入,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 即墨市| 疏勒县| 咸宁市| 公安县| 巴林右旗| 陇川县| 房山区| 启东市| 五峰| 横山县| 太康县| 双鸭山市| 司法| 阆中市| 靖宇县| 博白县| 雷州市| 大荔县| 株洲市| 吉木萨尔县| 于都县| 西宁市| 依兰县| 介休市| 新密市| 江川县| 抚顺县| 磐安县| 监利县| 泰州市| 新和县| 图们市| 瑞昌市| 郑州市| 石楼县| 兰西县| 特克斯县| 耒阳市| 金平| 江西省| 达州市|