來自于:http://hi.baidu.com/universeioi/blog/item/ab2ee5efab85493127979154.html
前面我們所定義的屬性都是幾本的屬性,如果我們定義一個屬性是Date類型,例如如下類中:
Java代碼 復制代碼
1. package com.szy.spring.bean;
2.
3. import java.util.Date;
4.
5. public class Bean {
6. private Date date;
7.
8. public Date getDate()
9. {
10. return date;
11. }
12. public void setDate(Date date)
13. {
14. this.date = date;
15. }
16. }
package com.szy.spring.bean;
import java.util.Date;
public class Bean {
private Date date;
public Date getDate()
{
return date;
}
public void setDate(Date date)
{
this.date = date;
}
}
按照我們以前學過的知識我們需要在配置文件中給該屬性注入值
Xml代碼 復制代碼
1. <bean id="bean" class="com.szy.spring.bean.Bean">
2. <property name="date" value="2009-11-21"/>
3. </bean>
<bean id="bean" class="com.szy.spring.bean.Bean">
<property name="date" value="2009-11-21"/>
</bean>
下面我們測試是否成功注入值
Java代碼 復制代碼
1. ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
2. Bean bean = (Bean)ctx.getBean("bean");
3. System.out.println(bean.getDate());
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
Bean bean = (Bean)ctx.getBean("bean");
System.out.println(bean.getDate());
運行包如下異常
Exception代碼 復制代碼
1. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bean' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property 'date'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'date': no matching editors or conversion strategy found
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bean' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property 'date'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'date': no matching editors or conversion strategy found
通過錯誤提示信息我們得知spring不能將string轉換成date類型,沒有匹配的編輯器或者轉換機制。
如果想實現string轉換成Date,那么我們自己需要寫一個屬性編輯器
我們新建一個類DatePropertyEditor,這個類要繼承PropertyEditorSupport類。
我們需要復寫這個類中的setAsText方法,其中text參數就是配置文件中的值。我們的任務就是把text轉換成date類型的值。
Java代碼 復制代碼
1. package com.szy.spring.util;
2.
3. import java.beans.PropertyEditorSupport;
4. import java.text.SimpleDateFormat;
5. import java.util.Date;
6.
7. public class DatePropertyEditor extends PropertyEditorSupport
8. {
9.
10. @Override
11. public void setAsText(String text) throws IllegalArgumentException
12. {
13. String format="yyyy-MM-dd";
14. SimpleDateFormat sdf=new SimpleDateFormat(format);
15. try
16. {
17. Date date=sdf.parse(text);
18. this.setValue(date); //把轉換后的值傳過去
19. } catch (Exception e)
20. {
21. e.printStackTrace();
22. }
23. }
24.
25. }
package com.szy.spring.util;
import java.beans.PropertyEditorSupport;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DatePropertyEditor extends PropertyEditorSupport
{
@Override
public void setAsText(String text) throws IllegalArgumentException
{
String format="yyyy-MM-dd";
SimpleDateFormat sdf=new SimpleDateFormat(format);
try
{
Date date=sdf.parse(text);
this.setValue(date); //把轉換后的值傳過去
} catch (Exception e)
{
e.printStackTrace();
}
}
}
寫完編輯器后我們還需要把編輯器注入到spring中。 為了方便管理我們再新建一個配置文件applicationEditor.xml,用來配置屬性編輯器
Xml代碼 復制代碼
1. <?xml version="1.0" encoding="UTF-8"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xmlns:context="http://www.springframework.org/schema/context"
5. xmlns:tx="http://www.springframework.org/schema/tx"
6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
9. <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
10. <!-- 把值注入到CustomEditorConfigurer的 Map類型的customEditors屬性-->
11. <property name="customEditors">
12. <map>
13. <entry key="java.util.Date">
14. <!-- 內部bean只供自己使用 -->
15. <bean class="com.szy.spring.util.DatePropertyEditor"/>
16. </entry>
17. </map>
18. </property>
19. </bean>
20.
21. </beans>
<?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:context="http://www.springframework.org/schema/context"
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.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<!-- 把值注入到CustomEditorConfigurer的 Map類型的customEditors屬性-->
<property name="customEditors">
<map>
<entry key="java.util.Date">
<!-- 內部bean只供自己使用 -->
<bean class="com.szy.spring.util.DatePropertyEditor"/>
</entry>
</map>
</property>
</bean>
</beans>
下面我們修改下測試代碼已讀取所有的配置文件
Java代碼 復制代碼
1. ApplicationContext ctx=new ClassPathXmlApplicationContext("application*.xml");
2. Bean bean = (Bean)ctx.getBean("bean");
3. System.out.println(bean.getDate());
ApplicationContext ctx=new ClassPathXmlApplicationContext("application*.xml");
Bean bean = (Bean)ctx.getBean("bean");
System.out.println(bean.getDate());
最后測試,成功輸出時間。
剛才我們在配置文件中時間的格式是2009-11-21,如果我們修改成2009/11/21呢?
運行報錯:Unparseable date: "2009/11/21"
這時我們需要修改屬性編輯器類文件的格式了,很麻煩。既然spring支持注入,那么我們為什么不對格式進行注入呢?
修改屬性編輯器類:
Java代碼 復制代碼
1. package com.szy.spring.util;
2.
3. import java.beans.PropertyEditorSupport;
4. import java.text.SimpleDateFormat;
5. import java.util.Date;
6.
7. public class DatePropertyEditor extends PropertyEditorSupport
8. {
9.
10. private String format;
11. @Override
12. public void setAsText(String text) throws IllegalArgumentException
13. {
14.
15. SimpleDateFormat sdf=new SimpleDateFormat(format);
16. try
17. {
18. Date date=sdf.parse(text);
19. this.setValue(date); //把轉換后的值傳過去
20. } catch (Exception e)
21. {
22. e.printStackTrace();
23. }
24. }
25. public String getFormat()
26. {
27. return format;
28. }
29. public void setFormat(String format)
30. {
31. this.format = format;
32. }
33. }
package com.szy.spring.util;
import java.beans.PropertyEditorSupport;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DatePropertyEditor extends PropertyEditorSupport
{
private String format;
@Override
public void setAsText(String text) throws IllegalArgumentException
{
SimpleDateFormat sdf=new SimpleDateFormat(format);
try
{
Date date=sdf.parse(text);
this.setValue(date); //把轉換后的值傳過去
} catch (Exception e)
{
e.printStackTrace();
}
}
public String getFormat()
{
return format;
}
public void setFormat(String format)
{
this.format = format;
}
}
同時給該類對應的bean添加屬性節點
Xml代碼 復制代碼
1. <bean class="com.szy.spring.util.DatePropertyEditor">
2. <property name="format" value="yyyy/MM/dd"></property>
3. </bean>
<bean class="com.szy.spring.util.DatePropertyEditor">
<property name="format" value="yyyy/MM/dd"></property>
</bean>
下次只要我們修改配置文件即可,靈活性很大。
Java代碼 復制代碼
1. package com.szy.spring.bean;
2.
3. import java.util.Date;
4.
5. public class Bean {
6. private Date date;
7.
8. public Date getDate()
9. {
10. return date;
11. }
12. public void setDate(Date date)
13. {
14. this.date = date;
15. }
16. }
package com.szy.spring.bean;
import java.util.Date;
public class Bean {
private Date date;
public Date getDate()
{
return date;
}
public void setDate(Date date)
{
this.date = date;
}
}
按照我們以前學過的知識我們需要在配置文件中給該屬性注入值
Xml代碼 復制代碼
1. <bean id="bean" class="com.szy.spring.bean.Bean">
2. <property name="date" value="2009-11-21"/>
3. </bean>
<bean id="bean" class="com.szy.spring.bean.Bean">
<property name="date" value="2009-11-21"/>
</bean>
下面我們測試是否成功注入值
Java代碼 復制代碼
1. ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
2. Bean bean = (Bean)ctx.getBean("bean");
3. System.out.println(bean.getDate());
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
Bean bean = (Bean)ctx.getBean("bean");
System.out.println(bean.getDate());
運行包如下異常
Exception代碼 復制代碼
1. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bean' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property 'date'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'date': no matching editors or conversion strategy found
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bean' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property 'date'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'date': no matching editors or conversion strategy found
通過錯誤提示信息我們得知spring不能將string轉換成date類型,沒有匹配的編輯器或者轉換機制。
如果想實現string轉換成Date,那么我們自己需要寫一個屬性編輯器
我們新建一個類DatePropertyEditor,這個類要繼承PropertyEditorSupport類。
我們需要復寫這個類中的setAsText方法,其中text參數就是配置文件中的值。我們的任務就是把text轉換成date類型的值。
Java代碼 復制代碼
1. package com.szy.spring.util;
2.
3. import java.beans.PropertyEditorSupport;
4. import java.text.SimpleDateFormat;
5. import java.util.Date;
6.
7. public class DatePropertyEditor extends PropertyEditorSupport
8. {
9.
10. @Override
11. public void setAsText(String text) throws IllegalArgumentException
12. {
13. String format="yyyy-MM-dd";
14. SimpleDateFormat sdf=new SimpleDateFormat(format);
15. try
16. {
17. Date date=sdf.parse(text);
18. this.setValue(date); //把轉換后的值傳過去
19. } catch (Exception e)
20. {
21. e.printStackTrace();
22. }
23. }
24.
25. }
package com.szy.spring.util;
import java.beans.PropertyEditorSupport;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DatePropertyEditor extends PropertyEditorSupport
{
@Override
public void setAsText(String text) throws IllegalArgumentException
{
String format="yyyy-MM-dd";
SimpleDateFormat sdf=new SimpleDateFormat(format);
try
{
Date date=sdf.parse(text);
this.setValue(date); //把轉換后的值傳過去
} catch (Exception e)
{
e.printStackTrace();
}
}
}
寫完編輯器后我們還需要把編輯器注入到spring中。 為了方便管理我們再新建一個配置文件applicationEditor.xml,用來配置屬性編輯器
Xml代碼 復制代碼
1. <?xml version="1.0" encoding="UTF-8"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xmlns:context="http://www.springframework.org/schema/context"
5. xmlns:tx="http://www.springframework.org/schema/tx"
6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
9. <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
10. <!-- 把值注入到CustomEditorConfigurer的 Map類型的customEditors屬性-->
11. <property name="customEditors">
12. <map>
13. <entry key="java.util.Date">
14. <!-- 內部bean只供自己使用 -->
15. <bean class="com.szy.spring.util.DatePropertyEditor"/>
16. </entry>
17. </map>
18. </property>
19. </bean>
20.
21. </beans>
<?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:context="http://www.springframework.org/schema/context"
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.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<!-- 把值注入到CustomEditorConfigurer的 Map類型的customEditors屬性-->
<property name="customEditors">
<map>
<entry key="java.util.Date">
<!-- 內部bean只供自己使用 -->
<bean class="com.szy.spring.util.DatePropertyEditor"/>
</entry>
</map>
</property>
</bean>
</beans>
下面我們修改下測試代碼已讀取所有的配置文件
Java代碼 復制代碼
1. ApplicationContext ctx=new ClassPathXmlApplicationContext("application*.xml");
2. Bean bean = (Bean)ctx.getBean("bean");
3. System.out.println(bean.getDate());
ApplicationContext ctx=new ClassPathXmlApplicationContext("application*.xml");
Bean bean = (Bean)ctx.getBean("bean");
System.out.println(bean.getDate());
最后測試,成功輸出時間。
剛才我們在配置文件中時間的格式是2009-11-21,如果我們修改成2009/11/21呢?
運行報錯:Unparseable date: "2009/11/21"
這時我們需要修改屬性編輯器類文件的格式了,很麻煩。既然spring支持注入,那么我們為什么不對格式進行注入呢?
修改屬性編輯器類:
Java代碼 復制代碼
1. package com.szy.spring.util;
2.
3. import java.beans.PropertyEditorSupport;
4. import java.text.SimpleDateFormat;
5. import java.util.Date;
6.
7. public class DatePropertyEditor extends PropertyEditorSupport
8. {
9.
10. private String format;
11. @Override
12. public void setAsText(String text) throws IllegalArgumentException
13. {
14.
15. SimpleDateFormat sdf=new SimpleDateFormat(format);
16. try
17. {
18. Date date=sdf.parse(text);
19. this.setValue(date); //把轉換后的值傳過去
20. } catch (Exception e)
21. {
22. e.printStackTrace();
23. }
24. }
25. public String getFormat()
26. {
27. return format;
28. }
29. public void setFormat(String format)
30. {
31. this.format = format;
32. }
33. }
package com.szy.spring.util;
import java.beans.PropertyEditorSupport;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DatePropertyEditor extends PropertyEditorSupport
{
private String format;
@Override
public void setAsText(String text) throws IllegalArgumentException
{
SimpleDateFormat sdf=new SimpleDateFormat(format);
try
{
Date date=sdf.parse(text);
this.setValue(date); //把轉換后的值傳過去
} catch (Exception e)
{
e.printStackTrace();
}
}
public String getFormat()
{
return format;
}
public void setFormat(String format)
{
this.format = format;
}
}
同時給該類對應的bean添加屬性節點
Xml代碼 復制代碼
1. <bean class="com.szy.spring.util.DatePropertyEditor">
2. <property name="format" value="yyyy/MM/dd"></property>
3. </bean>
<bean class="com.szy.spring.util.DatePropertyEditor">
<property name="format" value="yyyy/MM/dd"></property>
</bean>
下次只要我們修改配置文件即可,靈活性很大。