What is the attribute editor?
* Custom Attribute Editor. the strings in the spring configuration file are converted to corresponding objects for injection.
Spring already has a built-in property editor. We can customize the property editor as needed.
* How to define the Attribute Editor?
* Inherits the propertyeditorsupport class and overwrites the setastext () method. For details, see utildatepropertyeditor. java.
* Register the property editor to spring. For details, see applicationcontext. xml.
For example:
There is a class with a date attribute
Java code
- Public class bean1 {
- Private date datevalue;
- Public void setdatevalue (date datevalue ){
- This. datevalue = datevalue;
- }
- }
The applicationcontext. xml configuration file is as follows:
Java code
- <! -- Assign the value of date in bean1 to 2008-08-15. Spring considers 2008-08-15 to be a string and cannot be converted to date. An error is returned! -->
- <Bean id = "bean1" class = "com. bjsxt. Spring. bean1">
- <Property name = "datevalue">
- <Value> 2008-08-15 </value>
- </Property>
- </Bean>
- <! -- Defines the Attribute Editor -->
- <Bean id = "customeditorconfigurer" class = "org. springframework. Beans. Factory. config. customeditorconfigurer">
- <Property name = "customeditors">
- <Map>
- <Entry key = "Java. util. Date">
- <Bean class = "com. bjsxt. Spring. utildatepropertyeditor">
- <! -- Inject the format and flexibly process the format. -->
- <Property name = "format" value = "yyyy-mm-dd"/>
- </Bean>
- </Entry>
- </Map>
- </Property>
- </Bean>
Utildatepropertyeditor. Java is as follows. It must inherit the java. Beans. propertyeditorsupport class and overwrite the setastext () method.
Java code
- Import java. Beans. propertyeditorsupport;
- Import java. Text. parseexception;
- Import java. Text. simpledateformat;
- Import java. util. date;
- /**
- * Java. util. Date Attribute Editor
- * @ Author Administrator
- *
- */
- Public class utildatepropertyeditor extends propertyeditorsupport {
- Private string format = "yyyy-mm-dd ";
- @ Override
- Public void setastext (string text) throws illegalargumentexception {
- System. Out. println ("utildatepropertyeditor. saveastext () -- text =" + text );
- Simpledateformat SDF = new simpledateformat (format );
- Try {
- Date d = SDF. parse (text );
- This. setvalue (d );
- } Catch (parseexception e ){
- E. printstacktrace ();
- }
- }
- Public void setformat (string format ){
- This. format = format;
- }
- }
This completes the correct parsing. Note that customeditors is the attribute provided by spring customeditorconfigurer and is a map, which stores custom editors ), for example, the utildatepropertyeditor date editor is stored here. You can see the customeditorconfigurer source code.
Test:
Java code
- Import org. springframework. Beans. Factory. beanfactory;
- Import org. springframework. Context. Support. classpathxmlapplicationcontext;
- Import JUnit. Framework. testcase;
- Public class injectiontest extends testcase {
- Private beanfactory factory;
- @ Override
- Protected void setup () throws exception {
- Factory = new classpathxmlapplicationcontext ("applicationcontext. xml ");
- }
- Public void testinjection1 (){
- Bean1 bean1 = (bean1) Factory. getbean ("bean1 ");
- System. Out. println ("bean1.datevalue =" + bean1.getdatevalue ());
- }
- }
You can print the date.