This example also involves using multiple configuration files in Spring, and also the injection of Date Format ------- is more flexible.
Date Attribute Class: DatePropertyInjection. java
Copy codeThe Code is as follows: package com. zhmg. spring;
Import java. util. Date;
Public class DatePropertyInjection {
Private Date date;
Public Date getDate (){
Return date;
}
Public void setDate (Date date ){
This. date = date;
}
}
Attribute Editor class: PropertyEditor. javaCopy codeThe Code is as follows: package com. zhmg. spring;
Import java. beans. PropertyEditorSupport;
Import java. text. ParseException;
Import java. text. SimpleDateFormat;
Import java. util. Date;
/**
* The custom property editor processes attributes of the java. util. Date type.
* @ Author Administrator
*
*/
Public class PropertyEditor extends PropertyEditorSupport {
String format = "yyyy-MM-dd ";
Public void setFormat (String format ){
This. format = format;
}
@ Override
Public void setAsText (String arg0) throws IllegalArgumentException {
SimpleDateFormat dateFormat = new SimpleDateFormat (format );
Try {
Date date = dateFormat. parse (arg0 );
This. setValue (date );
} Catch (ParseException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}
ApplicationContextBeans. xmlCopy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: aop = "http://www.springframework.org/schema/aop"
Xmlns: tx = "http://www.springframework.org/schema/tx"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd ">
<Bean id = "dateProperty" class = "com. zhmg. spring. DatePropertyInjection">
<Property name = "date">
<Value> 2009-8-28 </value>
</Property>
</Bean>
</Beans>
ApplicationContextBeans. xmlCopy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: aop = "http://www.springframework.org/schema/aop"
Xmlns: tx = "http://www.springframework.org/schema/tx"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd ">
<! --
<Bean id = "dateProperty" class = "com. zhmg. spring. DatePropertyInjection">
<Property name = "date">
<Value> 2009-8-28 </value>
</Property>
</Bean>
-->
<Bean id = "editor" class = "org. springframework. beans. factory. config. CustomEditorConfigurer">
<Property name = "customEditors">
<Map>
<Entry key = "java. util. Date">
<Bean class = "com. zhmg. spring. PropertyEditor">
<! -Inject date format -->
<Property name = "format" value = "yyyy-MM-dd"/>
</Bean>
</Entry>
</Map>
</Property>
</Bean>
</Beans>
Unit: InjectionTest. javaCopy codeThe Code is as follows: package com. zhmg. spring;
Import junit. framework. TestCase;
Import org. springframework. beans. factory. BeanFactory;
Import org. springframework. context. support. ClassPathXmlApplicationContext;
Public class InjectionTest extends TestCase {
BeanFactory factory;
Protected void setUp () throws Exception {
// Read all configuration files starting with applicationContext using wildcards
Factory = new ClassPathXmlApplicationContext ("applicationContext *. xml ");
}
Public void testInjection (){
DatePropertyInjection dateProp = (DatePropertyInjection) factory. getBean ("dateProperty ");
System. out. println ("date =" + dateProp. getDate ());
}
}