当前位置: 移动技术网 > IT编程>开发语言>Java > Spring注入Date类型的三种方法总结

Spring注入Date类型的三种方法总结

2017年12月01日  | 移动技术网IT编程  | 我要评论

spring注入date类型的三种方法总结

测试bean:

public class datebean { 
  private date birthday; 
 
  public date getbirthday() { 
    return birthday; 
  } 
 
  public void setbirthday(date birthday) { 
    this.birthday = birthday; 
  } 
} 

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

<?xml version="1.0" encoding="utf-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 
  xsi:schemalocation=" 
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans.xsd"> 
 
  <bean id="dateformat" class="java.text.simpledateformat"> 
  <constructor-arg value="yyyy-mm-dd" /> 
  </bean> 
   
  <bean id="datebean" class="com.springdemo1.date类型注入.datebean"> 
    <property name="birthday"> 
      <bean factory-bean="dateformat" factory-method="parse"> 
        <constructor-arg value="2015-12-31" /> 
      </bean> 
    </property> 
  </bean> 
</beans> 

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

<?xml version="1.0" encoding="utf-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 
  xsi:schemalocation=" 
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans.xsd"> 
 
   
  <!-- 自定义日期编辑器 --> 
  <bean id="dateeditor" 
    class="org.springframework.beans.propertyeditors.customdateeditor"> 
    <constructor-arg> 
      <bean class="java.text.simpledateformat"> 
        <constructor-arg value="yyyy-mm-dd"></constructor-arg> 
      </bean> 
    </constructor-arg> 
    <constructor-arg value="true" /> 
  </bean> 
  <!-- 使 spring转换为java.util.date --> 
  <bean class="org.springframework.beans.factory.config.customeditorconfigurer"> 
    <property name="customeditors"> 
      <map> 
        <entry key="java.util.date"> 
          <ref bean="dateeditor" /> 
        </entry> 
      </map> 
    </property> 
  </bean> 
</beans> 

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

public class mydatepropertyeditor extends propertyeditorsupport { 
  private string format; 
 
  public string getformat() { 
    return format; 
  } 
 
  public void setformat(string format) { 
    this.format = format; 
  } 
 
  // text为需要转换的值,当为bean注入的类型与编辑器转换的类型匹配时就会交给setastext方法处理 
  public void setastext(string text) throws illegalargumentexception { 
    simpledateformat sdf = new simpledateformat(format); 
    try { 
      this.setvalue(sdf.parse(text)); 
    } catch (parseexception e) { 
      e.printstacktrace(); 
    } 
  } 
} 

<?xml version="1.0" encoding="utf-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 
  xsi:schemalocation=" 
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans.xsd"> 
 
  <!--方式3:创建一个类 重写propertyeditorsupport的setastext方法 --> 
  <!-- 自定义日期编辑器 --> 
  <bean class="org.springframework.beans.factory.config.customeditorconfigurer"> 
    <property name="customeditors"> <!--需要编辑的属性类型,是一个map --> 
      <map> 
        <entry key="java.util.date"> 
          <bean class="com.springdemo1.date类型注入.mydatepropertyeditor"> 
            <property name="format" value="yyyy-mm-dd" /> <!--注入需要转换的格式 --> 
          </bean> 
        </entry> 
      </map> 
    </property> 
  </bean> 
</beans> 

测试:

public class datetest { 
  @test 
  public void testname() throws exception { 
     
    applicationcontext context = new classpathxmlapplicationcontext( 
        "applicationcontext.xml"); 
     
    datebean bean = (datebean) context.getbean("datebean"); 
    system.out.println(bean.getbirthday()); 
  } 
} 

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网