源码网,源码论坛,源码之家,商业源码,游戏源码下载,discuz插件,棋牌源码下载,精品源码论坛

 找回密码
 立即注册
查看: 500|回复: 8

[JSP编程] 通过Setters方式对日期属性及日期格式进行IOC注入

[复制链接]

7万

主题

861

回帖

32万

积分

论坛元老

Rank: 8Rank: 8

积分
329525
发表于 2009-8-29 00:38:41 | 显示全部楼层 |阅读模式
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/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());
}
}
回复

使用道具 举报

1

主题

1万

回帖

93

积分

注册会员

Rank: 2

积分
93
发表于 2023-2-5 09:24:14 | 显示全部楼层
呵呵呵呵呵呵
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

100

积分

注册会员

Rank: 2

积分
100
发表于 2023-2-6 16:46:04 | 显示全部楼层
你们谁看了弄洒了可能
回复 支持 反对

使用道具 举报

4

主题

1万

回帖

107

积分

注册会员

Rank: 2

积分
107
发表于 2023-4-14 02:14:37 | 显示全部楼层
啊啊啊啊啊啊啊啊啊啊啊啊啊啊
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

87

积分

注册会员

Rank: 2

积分
87
发表于 2023-7-26 16:36:14 | 显示全部楼层
啊,数码撒飒飒飒飒
回复 支持 反对

使用道具 举报

1

主题

1万

回帖

55

积分

注册会员

Rank: 2

积分
55
发表于 2024-5-13 21:35:22 | 显示全部楼层
可以,看卡巴
回复 支持 反对

使用道具 举报

1

主题

1万

回帖

307

积分

中级会员

Rank: 3Rank: 3

积分
307
发表于 2024-6-6 00:22:34 | 显示全部楼层
哈哈哈哈哈哈
回复 支持 反对

使用道具 举报

3

主题

5983

回帖

50

积分

注册会员

Rank: 2

积分
50
发表于 2024-7-12 16:40:00 | 显示全部楼层
建军节建军节建军节建军节
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2024-9-5 18:25:52 | 显示全部楼层
女生看了弄丢了卡萨诺的卡洛斯
回复 支持 反对

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

手机版|小黑屋|网站地图|源码论坛 ( 海外版 )

GMT+8, 2024-9-20 14:48 , Processed in 0.075461 second(s), 26 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表