Custom Attribute Editor in Spring: customeditorconfigurer

Source: Internet
Author: User

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  
  1. Public class bean1 {
  2. Private date datevalue;
  3. Public void setdatevalue (date datevalue ){
  4. This. datevalue = datevalue;
  5. }
  6. }


The applicationcontext. xml configuration file is as follows:

Java code  
  1. <! -- 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! -->
  2. <Bean id = "bean1" class = "com. bjsxt. Spring. bean1">
  3. <Property name = "datevalue">
  4. <Value> 2008-08-15 </value>
  5. </Property>
  6. </Bean>
  7. <! -- Defines the Attribute Editor -->
  8. <Bean id = "customeditorconfigurer" class = "org. springframework. Beans. Factory. config. customeditorconfigurer">
  9. <Property name = "customeditors">
  10. <Map>
  11. <Entry key = "Java. util. Date">
  12. <Bean class = "com. bjsxt. Spring. utildatepropertyeditor">
  13. <! -- Inject the format and flexibly process the format. -->
  14. <Property name = "format" value = "yyyy-mm-dd"/>
  15. </Bean>
  16. </Entry>
  17. </Map>
  18. </Property>
  19. </Bean>


Utildatepropertyeditor. Java is as follows. It must inherit the java. Beans. propertyeditorsupport class and overwrite the setastext () method.

Java code  
  1. Import java. Beans. propertyeditorsupport;
  2. Import java. Text. parseexception;
  3. Import java. Text. simpledateformat;
  4. Import java. util. date;
  5. /**
  6. * Java. util. Date Attribute Editor
  7. * @ Author Administrator
  8. *
  9. */
  10. Public class utildatepropertyeditor extends propertyeditorsupport {
  11. Private string format = "yyyy-mm-dd ";
  12. @ Override
  13. Public void setastext (string text) throws illegalargumentexception {
  14. System. Out. println ("utildatepropertyeditor. saveastext () -- text =" + text );
  15. Simpledateformat SDF = new simpledateformat (format );
  16. Try {
  17. Date d = SDF. parse (text );
  18. This. setvalue (d );
  19. } Catch (parseexception e ){
  20. E. printstacktrace ();
  21. }
  22. }
  23. Public void setformat (string format ){
  24. This. format = format;
  25. }
  26. }


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  
  1. Import org. springframework. Beans. Factory. beanfactory;
  2. Import org. springframework. Context. Support. classpathxmlapplicationcontext;
  3. Import JUnit. Framework. testcase;
  4. Public class injectiontest extends testcase {
  5. Private beanfactory factory;
  6. @ Override
  7. Protected void setup () throws exception {
  8. Factory = new classpathxmlapplicationcontext ("applicationcontext. xml ");
  9. }
  10. Public void testinjection1 (){
  11. Bean1 bean1 = (bean1) Factory. getbean ("bean1 ");
  12. System. Out. println ("bean1.datevalue =" + bean1.getdatevalue ());
  13. }
  14. }


You can print the date.

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.