SoFunction
Updated on 2025-04-05

Summary of three methods of Spring Injection Date Type

Summary of three methods of Spring Injection Date Type

Test Bean:

public class DateBean { 
  private Date birthday; 
 
  public Date getBirthday() { 
    return birthday; 
  } 
 
  public void setBirthday(Date birthday) { 
     = birthday; 
  } 
} 

Method 1: Inject using SimpleDateFormat's constructor method

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="/schema/beans" 
  xmlns:p="/schema/p" xmlns:xsi="http:///2001/XMLSchema-instance" 
  xsi:schemaLocation=" 
    /schema/beans  
    /schema/beans/"> 
 
  <bean  class=""> 
  <constructor-arg value="yyyy-MM-dd" /> 
  </bean> 
   
  <bean  class="com.type injection.DateBean"> 
    <property name="birthday"> 
      <bean factory-bean="dateFormat" factory-method="parse"> 
        <constructor-arg value="2015-12-31" /> 
      </bean> 
    </property> 
  </bean> 
</beans> 

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

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="/schema/beans" 
  xmlns:p="/schema/p" xmlns:xsi="http:///2001/XMLSchema-instance" 
  xsi:schemaLocation=" 
    /schema/beans  
    /schema/beans/"> 
 
   
  <!-- Custom date editor --> 
  <bean  
    class=""> 
    <constructor-arg> 
      <bean class=""> 
        <constructor-arg value="yyyy-MM-dd"></constructor-arg> 
      </bean> 
    </constructor-arg> 
    <constructor-arg value="true" /> 
  </bean> 
  <!-- make SpringConvert to --> 
  <bean class=""> 
    <property name="customEditors"> 
      <map> 
        <entry key=""> 
          <ref bean="dateEditor" /> 
        </entry> 
      </map> 
    </property> 
  </bean> 
</beans> 

Method 3: First use a class to rewrite the setAsText method of PropertyEditorSupport, and then in the configuration file, just configure the conversion type, which is similar to the above method.

public class MyDatePropertyEditor extends PropertyEditorSupport { 
  private String format; 
 
  public String getFormat() { 
    return format; 
  } 
 
  public void setFormat(String format) { 
     = format; 
  } 
 
  // text is the value that needs to be converted. When the type injected to the bean matches the type converted by the editor, it will be handed over to the setAsText method for processing.  public void setAsText(String text) throws IllegalArgumentException { 
    SimpleDateFormat sdf = new SimpleDateFormat(format); 
    try { 
      ((text)); 
    } catch (ParseException e) { 
      (); 
    } 
  } 
} 

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="/schema/beans" 
  xmlns:p="/schema/p" xmlns:xsi="http:///2001/XMLSchema-instance" 
  xsi:schemaLocation=" 
    /schema/beans  
    /schema/beans/"> 
 
  <!--Way3:Create a class RewritePropertyEditorSupportofsetAsTextmethod --> 
  <!-- Custom date editor --> 
  <bean class=""> 
    <property name="customEditors"> <!--需要编辑of属性类型,It's onemap --> 
      <map> 
        <entry key=""> 
          <bean class="com.type injection.MyDatePropertyEditor"> 
            <property name="format" value="yyyy-MM-dd" /> <!--注入需要转换of格式 --> 
          </bean> 
        </entry> 
      </map> 
    </property> 
  </bean> 
</beans> 

test:

public class DateTest { 
  @Test 
  public void testName() throws Exception { 
     
    ApplicationContext context = new ClassPathXmlApplicationContext( 
        ""); 
     
    DateBean bean = (DateBean) ("datebean"); 
    (()); 
  } 
} 

If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!