Spring configuration file

Source: Internet
Author: User

Custom type converters (example is the date type of the conversion)

1. New type conversion class Dateconvert.java and inherit Java.beans.PropertyEditorSupport

2. Rewrite the Setastext (String text) method

 Public classDateconvertextendsPropertyEditorSupport {@Override Public voidSetastext (String text)throwsillegalargumentexception {simpledateformat sdf=NewSimpleDateFormat ("Yyyy-mm-dd"); Try{Date Date=sdf.parse (text);  This. SetValue (date); } Catch(ParseException e) {e.printstacktrace (); }    }}

3. Configure the type converter in the configuration file

<Beanname= "Customeditor"class= "Org.springframework.beans.factory.config.CustomEditorConfigurer">              < Propertyname= "Customeditors" >                     <Map>                            <entryKey= "Java.util.Date"value= "Com.tidus.spring.util.DateConvert"/>                     </Map>              </ Property>       </Bean>

4, then you can inject the date type into the model

<Beanname= "Studentservice"class= "Com.tidus.spring.service.StudentService">              < Propertyname= "DAO"ref= "Studentdao" />              < Propertyname= "Studentlist">                     <List>                            <Beanname= "S1"class= "Com.tidus.spring.model.Student" >                                   < Propertyname= "Name"value= "Zhang San" />                                   < Propertyname= "id"value= "0" />                                   < Propertyname= "Age"value= " the" />                                   < Propertyname= "Birthday"value= "1987-05-08" />                            </Bean>                            <Beanname= "S2"class= "Com.tidus.spring.model.Student" >                                   < Propertyname= "Name"value= "Zhang 4" />                                   < Propertyname= "id"value= "1" />                                   < Propertyname= "Age"value= "All" />                                   < Propertyname= "Birthday"value= "1985-03-08" />                            </Bean>                            <Beanname= "S3"class= "Com.tidus.spring.model.Student" >                                   < Propertyname= "Name"value= "Zhang 5" />                                   < Propertyname= "id"value= "2" />                                   < Propertyname= "Age"value= "+" />                                   < Propertyname= "Birthday"value= "1987-06-08" />                            </Bean>                     </List>              </ Property>       </Bean>

Spring configuration file

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.