Summary of three methods for Spring injection of the Date type, springdate

Source: Internet
Author: User

Summary of three methods for Spring injection of the Date type, springdate

Summary of three methods for Spring injection of the 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: inject using SimpleDateFormat's constructor

<? 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 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: configuration-only, custom CustomDateEditor first, and then type conversion

<? 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 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> <! -- Convert Spring to 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>

Method 3: first use a class to override the setAsText method of PropertyEditorSupport. Then, in the configuration file, configure the conversion type, which is similar to the preceding 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 bean injection type matches the type converted by the editor, the setAsText method is used to process 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 to override the setAsText method of PropertyEditorSupport --> <! -- Custom date editor --> <bean class = "org. springframework. beans. factory. config. CustomEditorConfigurer"> <property name = "customEditors"> <! -- The attribute type to be edited is a map --> <map> <entry key = "java. util. date "> <bean class =" com. springDemo1.Date type injection. myDatePropertyEditor "> <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());   } } 

If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article. Thank you for your support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.