Home>

Summary of three methods for spring injection date type

Test bean:

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

Method 1:Constructed method injection using simplifiedateformat

<?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>
  <constructor-arg value="yyyy-mm-dd" />
  </bean>
  <bean>
    <property name="birthday">
      <bean factory-bean="dateformat" factory-method="parse">
        <constructor-arg value="2015-12-31" />
      </bean>
    </property>
  </bean>
</beans>

Method 2:Pure configuration, customize customdateeditor first, and then convert the type

<?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">
  <!-Custom Date Editor->
  <bean
   >
    <constructor-arg>
      <bean>
        <constructor-arg value="yyyy-mm-dd"></constructor-arg>
      </bean>
    </constructor-arg>
    <constructor-arg value="true" />
  </bean>
  <!-Make spring converted to java.util.date->
  <bean>
    <property name="customeditors">
      <map>
        <entry key="java.util.date">
          <ref bean="dateeditor" />
        </entry>
      </map>
    </property>
  </bean>
</beans>

Method 3:First override the setastext method of propertyeditorsupport with a class, and then in the configuration file,Just configure the conversion type,Similar to the above method

public class mydatepropertyeditor extends propertyeditorsupport {
  private string format;
  public string getformat () {
    return format;
  }
  public void setformat (string format) {
    this.format=format;
  }
  //text is the value to be converted,When the type injected into the bean matches the type converted by the editor, it is passed to the setastext method.
  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">
  <!-Method 3:Create a class Override the setastext method of propertyeditorsupport->
  <!-Custom Date Editor->
  <bean>
    <property name="customeditors"><!-The type of property i want to edit,Is a map->
      <map>
        <entry key="java.util.date">
          <bean>
            <property name="format" value="yyyy-mm-dd" /><!-Inject the format to be converted->
          </bean>
        </entry>
      </map>
    </property>
  </bean>
</beans>

test:

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 ());
  }
}
jsp
  • Previous A small music player with native JS
  • Next AngularJS implements registration form validation
  • Trends