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
- Package com. cos. entity;
- Import java. util. Date;
- Import java. util. List;
- Import java. util. Map;
- Import java. util. Set;
- Public class UserBean {
- Private Date birthday;
- Public Date getBirthday (){
- Return birthday;
- }
- Public void setBirthday (Date birthday ){
- This. birthday = birthday;
- }
- }
Custom property Editor
Java code
- Package com. cos. entity;
- Import java. beans. PropertyEditorSupport;
- Import java. text. ParseException;
- Import java. text. SimpleDateFormat;
- // Write a custom property editor to inherit the property editor PropertyEditorSupport
- Public class DatePropertyEditor extends PropertyEditorSupport {
- // Time format
- String format;
- Public String getFormat (){
- Return format;
- }
- Public void setFormat (String format ){
- This. format = format;
- }
- // You need to override the setAsText () method of the property editor.
- @ Override
- Public void setAsText (String text ){
- Try {
- SimpleDateFormat f = new SimpleDateFormat (format );
- // Pass the converted value
- This. setValue (f. parse (text ));
- } Catch (ParseException ex ){
- Ex. printStackTrace ();
- }
- }
- }
Spring configuration file applicationContext. xml:
Xml Code
- <Beans xmlns = "http://www.springframework.org/schema/beans"
- Xmlns: jdbc = "http://www.springframework.org/schema/jdbc"
- Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
- Xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- Http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd ">
- <! -- Common property injection -->
- <Bean id = "userBean" class = "com. cos. entity. UserBean">
- <! -- Time attribute. The Attribute Editor is required. -->
- <Property name = "birthday" value = "2011-03-16"/>
- </Bean>
- <! -- Inject special attributes into CustomEditorConfigurer Bean -->
- <Bean class = "org. springframework. beans. factory. config. CustomEditorConfigurer">
- <Property name = "customEditors">
- <Map>
- <Entry key = "java. util. Date">
- <Bean class = "com. cos. entity. DatePropertyEditor">
- <Property name = "format" value = "yyyy-MM-dd"/>
- </Bean>
- </Entry>
- </Map>
- </Property>
- </Bean>
- </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
- Package com. cos. entity;
- Import org. springframework. beans. factory. BeanFactory;
- Import org. springframework. context. support. ClassPathXmlApplicationContext;
- Public class Main {
- Public static void main (String [] args ){
- // Return the Bean Factory object through the spring configuration file
- BeanFactory factory = new ClassPathXmlApplicationContext ("applicationContext. xml ");
- // Bean factory obtains JavaBean by Bean id
- UserBean ub = (UserBean) factory. getBean ("userBean ");
- System. out. println ("" + ub. getBirthday ());
- }
- }