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!