IOC容器之灵活配置对象属性值二->Date

      上篇博客( IOC容器之灵活配置对象属性值一)主要介绍了如何通过spring中配置文件给的对象赋值,接下来我们介绍一个比较特殊的数据类型,看它如何实现在Sping容器中的灵活配置。

一.spring中String类型与Date的转换问题

java代码:

<span style="font-family:SimSun;font-size:18px;">		package com.huxj.spring;
		import java.util.Date;
		
		public class Bean1 {				
			private Date dateValue;
			public Date getDateValue() {
				return dateValue;
			}
			public void setDateValue(Date dateValue) {
				this.dateValue = dateValue;
			}
		}</span>

    applicationContext.xml:
<span style="font-family:SimSun;font-size:18px;">		<?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="bean1" class="com.huxj.spring.Bean1">
				<property name="dateValue" value="2015/065/06" />
		
			</bean>
		
		</beans>
		
</span>

测试用例:
<span style="font-family:SimSun;font-size:18px;"><strong style="font-size: 14.6666669845581px; color: rgb(54, 96, 146); font-family: 宋体; background-color: rgb(255, 255, 255);"></strong></span>
<span style="font-family:SimSun;font-size:18px;">		测试用例:
		package test;
		
		import org.springframework.beans.factory.BeanFactory;
		import org.springframework.context.support.ClassPathXmlApplicationContext;
		
		import com.huxj.spring.Bean1;
		
		import junit.framework.TestCase;
		
		public class InjectionTest extends TestCase {
		
			BeanFactory beanFactory;
			//初始化,只执行一次
			@Override
			protected void setUp() throws Exception {
				beanFactory=new ClassPathXmlApplicationContext("applicationContext.xml");
			}
			
			//测试方法
			public void testInjection1(){
				Bean1 bean1=(Bean1)beanFactory.getBean("bean1");
				System.out.print("bean1.mapValue="+bean1.getDateValue()+"\n");
			}
		
		}
		
</span>

结果报错:
PropertyAccessException1: org.springframework.beans.TypeMismatchException: Failed to convert propertyvalue of type [java.lang.String] to required type [java.util.Date] for property'dateValue'; nested exception is java.lang.IllegalArgumentException: Nomatching editors or conversion strategy found

错误分析:
我们知道读取配置文件的数据类型都是spring类型的,一般类型spring都能提供自动转换功能。但是Date类型呢,由于Date的格式(yyyy-MM-dd 或 yyyy/MM/dd等)不确定,所以spring没有提供自动此类型转换才报如上错误。

二、如何解决spring中String类型与Date的转换问题

1、创建类UtilDatePropertyEditor实现PropertyEditorSupport接口,在其中进行类型转换。
package com.huxj.spring;
		
		import java.beans.PropertyEditorSupport;
		import java.text.ParseException;
		import java.text.SimpleDateFormat;
		import java.util.Date;
		
		public class UtilDatePropertyEditor extends PropertyEditorSupport {
			
			private String pattern;
			//通过setter在spring进行配置,即可注入匹配的数据格式
			public void setPattern(String pattern) {
				this.pattern = pattern;
			}
		
			//从配置文件中获取时间text
			@Override
			public void setAsText(String text) throws IllegalArgumentException {
				System.out.println("--------UtilDatePropertyEditor.setAsText-------------"+text);
				
				try {
					//数据格式的转换
					Date date=new SimpleDateFormat(pattern).parse(text);
					//需要进行一下存储
					this.setValue(date);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					throw new IllegalArgumentException(text);
				}
				
			}
			
		
		}

2.创建编辑器配置文件applicationContext-editor.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">
			
				<!-- 编辑器给注册到Spring中 -->
				<bean id="customEditors"
					class="org.springframework.beans.factory.config.CustomEditorConfigurer">
					<!-- 编辑器放入到map当中,map中的key是类型,value="编辑器类" -->
					<property name="customEditors">
						<map>
							<entry key="java.util.Date">
								<!-- 这里是内部bean,所以不用提供id -->
								<bean class="com.huxj.spring.UtilDatePropertyEditor">
									<property name="pattern" value="2015/09/06"></property>
								</bean>
							</entry>
						</map>
					</property>
				</bean>
			</beans>
注意:此配置中的”<property name="pattern" value="2015/09/05"></property>“一定要和applicationContext.xml中设置Date属性格式数值一定要一致。要想修改时间格式,两处都需要修改。

3.测试用例进行测试:
			public class InjectionTest extends TestCase {
			
				BeanFactory beanFactory;
				//相当于初始化,只执行一次
				@Override
				protected void setUp() throws Exception {
					//读取多个xml文件
					
					//第一种方式,以数组形式传参
					//String[] configLocations=new String[]{"applicationContext-beans.xml","applicationContext-editor.xml"};
					//beanFactory=new ClassPathXmlApplicationContext(configLocations);
					//第二种方式,服从约定大于配置的原则。
					beanFactory=new ClassPathXmlApplicationContext("applicationContext-*.xml");
				}
				//测试方法
				public void testInjection1(){
					System.out.print("bean1.dateValue="+bean1.getDateValue()+"\n");
				}
			
			}

当我采用”beanFactory=newClassPathXmlApplicationContext("applicationContext-*.xml");“这种方式的时候,报错:
java.lang.IllegalArgumentException:Resource path[E:\鎻愰珮鐝�\瀛︿範\璁$畻鏈�\Java\SSH瑙嗛_鐜嬪媷\浠g爜\Spring\spring_injection\bin] does notdenote a directory。
解决方案:将此测试用例java的编码方式转化成”UTF-8“格式的。

总结:

不管我们遇到什么问题,请相信办法总比困难多。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值