Spring custom property Editor

Source: Internet
Author: User

Spring custom property Editor

Normal attributes can be injected during Spring DI injection, but those of the Date type cannot be identified. In this case, you can use Spring property editor to convert the strings in the configuration file into corresponding objects for injection.

Spring has its own Attribute Editor. We can also write a custom attribute editor.

Custom property Editor:

Inherit from the java. beans. PropertyEditorSupport class and override the setAsText (String text) method.

Then, inject the custom property editor into Spring.

Example:

JavaBean class

Java code

 

 

  1. Package com. cos. entity;
  2. Import java. util. Date;
  3. Import java. util. List;
  4. Import java. util. Map;
  5. Import java. util. Set;
  6. Public class UserBean {
  7. Private Date birthday;
  8. Public Date getBirthday (){
  9. Return birthday;
  10. }
  11. Public void setBirthday (Date birthday ){
  12. This. birthday = birthday;
  13. }
  14. }

 

Custom property Editor

Java code

 

 

  1. Package com. cos. entity;
  2. Import java. beans. PropertyEditorSupport;
  3. Import java. text. ParseException;
  4. Import java. text. SimpleDateFormat;
  5. // Write a custom property editor to inherit the property editor PropertyEditorSupport
  6. Public class DatePropertyEditor extends PropertyEditorSupport {
  7. // Time format
  8. String format;
  9. Public String getFormat (){
  10. Return format;
  11. }
  12. Public void setFormat (String format ){
  13. This. format = format;
  14. }
  15. // You need to override the setAsText () method of the property editor.
  16. @ Override
  17. Public void setAsText (String text ){
  18. Try {
  19. SimpleDateFormat f = new SimpleDateFormat (format );
  20. // Pass the converted value
  21. This. setValue (f. parse (text ));
  22. } Catch (ParseException ex ){
  23. Ex. printStackTrace ();
  24. }
  25. }
  26. }

 

Spring configuration file applicationContext. xml:

Xml Code

 

  1. <Beans xmlns = "http://www.springframework.org/schema/beans"
  2. Xmlns: jdbc = "http://www.springframework.org/schema/jdbc"
  3. Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
  4. Xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  5. Http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd ">
  6. <! -- Common property injection -->
  7. <Bean id = "userBean" class = "com. cos. entity. UserBean">
  8. <! -- Time attribute. The Attribute Editor is required. -->
  9. <Property name = "birthday" value = "2011-03-16"/>
  10. </Bean>
  11. <! -- Inject special attributes into CustomEditorConfigurer Bean -->
  12. <Bean class = "org. springframework. beans. factory. config. CustomEditorConfigurer">
  13. <Property name = "customEditors">
  14. <Map>
  15. <Entry key = "java. util. Date">
  16. <Bean class = "com. cos. entity. DatePropertyEditor">
  17. <Property name = "format" value = "yyyy-MM-dd"/>
  18. </Bean>
  19. </Entry>
  20. </Map>
  21. </Property>
  22. </Bean>
  23. </Beans>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The org. springframework. beans. factory. config. medmeditorconfigurer class can read the PropertyEditorSupport class and subclass, and convert the string to the specified type.

The PropertyEditorSupport class injects the Date type to be converted into customEditors Map.

Test class:

Java code

 

 

  1. Package com. cos. entity;
  2. Import org. springframework. beans. factory. BeanFactory;
  3. Import org. springframework. context. support. ClassPathXmlApplicationContext;
  4. Public class Main {
  5. Public static void main (String [] args ){
  6. // Return the Bean Factory object through the spring configuration file
  7. BeanFactory factory = new ClassPathXmlApplicationContext ("applicationContext. xml ");
  8. // Bean factory obtains JavaBean by Bean id
  9. UserBean ub = (UserBean) factory. getBean ("userBean ");
  10. System. out. println ("" + ub. getBirthday ());
  11. }
  12. }

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.