Spring注入Date类型的3种方式

测试Bean:

[html]  view plain  copy
  1. public class DateBean {  
  2.     private Date birthday;  
  3.   
  4.     public Date getBirthday() {  
  5.         return birthday;  
  6.     }  
  7.   
  8.     public void setBirthday(Date birthday) {  
  9.         this.birthday = birthday;  
  10.     }  
  11. }  

方式1:利用SimpleDateFormat的构造方法注入

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="  
  5.         http://www.springframework.org/schema/beans   
  6.         http://www.springframework.org/schema/beans/spring-beans.xsd">  
  7.   
  8.     <bean id="dateFormat" class="java.text.SimpleDateFormat">  
  9.     <constructor-arg value="yyyy-MM-dd" />  
  10.     </bean>  
  11.      
  12.     <bean id="datebean" class="com.springDemo1.Date类型注入.DateBean">  
  13.         <property name="birthday">  
  14.             <bean factory-bean="dateFormat" factory-method="parse">  
  15.                 <constructor-arg value="2015-12-31" />  
  16.             </bean>  
  17.         </property>  
  18.     </bean>  
  19. </beans>  


方式2:纯配置,先自定义CustomDateEditor,再转换类型

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="  
  5.         http://www.springframework.org/schema/beans   
  6.         http://www.springframework.org/schema/beans/spring-beans.xsd">  
  7.   
  8.       
  9.     <!-- 自定义日期编辑器 -->  
  10.     <bean id="dateEditor"  
  11.         class="org.springframework.beans.propertyeditors.CustomDateEditor">  
  12.         <constructor-arg>  
  13.             <bean class="java.text.SimpleDateFormat">  
  14.                 <constructor-arg value="yyyy-MM-dd"></constructor-arg>  
  15.             </bean>  
  16.         </constructor-arg>  
  17.         <constructor-arg value="true" />  
  18.     </bean>  
  19.     <!-- 使 Spring转换为java.util.Date -->  
  20.     <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">  
  21.         <property name="customEditors">  
  22.             <map>  
  23.                 <entry key="java.util.Date">  
  24.                     <ref bean="dateEditor" />  
  25.                 </entry>  
  26.             </map>  
  27.         </property>  
  28.     </bean>  
  29. </beans>  

方式3:先用一个类重写PropertyEditorSupport的setAsText方法,再在配置文件中,配置转换类型就可以了,跟上面方法类似

[html]  view plain  copy
  1. public class MyDatePropertyEditor extends PropertyEditorSupport {  
  2.     private String format;  
  3.   
  4.     public String getFormat() {  
  5.         return format;  
  6.     }  
  7.   
  8.     public void setFormat(String format) {  
  9.         this.format = format;  
  10.     }  
  11.   
  12.     // text为需要转换的值,当为bean注入的类型与编辑器转换的类型匹配时就会交给setAsText方法处理  
  13.     public void setAsText(String text) throws IllegalArgumentException {  
  14.         SimpleDateFormat sdf = new SimpleDateFormat(format);  
  15.         try {  
  16.             this.setValue(sdf.parse(text));  
  17.         } catch (ParseException e) {  
  18.             e.printStackTrace();  
  19.         }  
  20.     }  
  21. }  


[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="  
  5.         http://www.springframework.org/schema/beans   
  6.         http://www.springframework.org/schema/beans/spring-beans.xsd">  
  7.   
  8.     <!--方式3:创建一个类 重写PropertyEditorSupport的setAsText方法 -->  
  9.     <!-- 自定义日期编辑器 -->  
  10.     <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">  
  11.         <property name="customEditors">  <!--需要编辑的属性类型,是一个map -->  
  12.             <map>  
  13.                 <entry key="java.util.Date">  
  14.                     <bean class="com.springDemo1.Date类型注入.MyDatePropertyEditor">  
  15.                         <property name="format" value="yyyy-MM-dd" />  <!--注入需要转换的格式 -->  
  16.                     </bean>  
  17.                 </entry>  
  18.             </map>  
  19.         </property>  
  20.     </bean>  
  21. </beans>  


测试:

[html]  view plain  copy
  1. public class DateTest {  
  2.     @Test  
  3.     public void testName() throws Exception {  
  4.           
  5.         ApplicationContext context = new ClassPathXmlApplicationContext(  
  6.                 "applicationContext.xml");  
  7.           
  8.         DateBean bean = (DateBean) context.getBean("datebean");  
  9.         System.out.println(bean.getBirthday());  
  10.     }  
  11. }  

猜你喜欢

转载自blog.csdn.net/weixin_41562778/article/details/80372365