IOC injection for date attributes and date formats using Setters

Source: Internet
Author: User
Tags dateformat

This example also involves using multiple configuration files in Spring, and also the injection of Date Format ------- is more flexible.
Date Attribute Class: DatePropertyInjection. java Copy codeThe Code is as follows: package com. zhmg. spring;
Import java. util. Date;
Public class DatePropertyInjection {
Private Date date;
Public Date getDate (){
Return date;
}
Public void setDate (Date date ){
This. date = date;
}
}

Attribute Editor class: PropertyEditor. javaCopy codeThe Code is as follows: package com. zhmg. spring;
Import java. beans. PropertyEditorSupport;
Import java. text. ParseException;
Import java. text. SimpleDateFormat;
Import java. util. Date;
/**
* The custom property editor processes attributes of the java. util. Date type.
* @ Author Administrator
*
*/
Public class PropertyEditor extends PropertyEditorSupport {
String format = "yyyy-MM-dd ";
Public void setFormat (String format ){
This. format = format;
}
@ Override
Public void setAsText (String arg0) throws IllegalArgumentException {
SimpleDateFormat dateFormat = new SimpleDateFormat (format );
Try {
Date date = dateFormat. parse (arg0 );
This. setValue (date );
} Catch (ParseException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}

ApplicationContextBeans. xmlCopy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: aop = "http://www.springframework.org/schema/aop"
Xmlns: tx = "http://www.springframework.org/schema/tx"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd ">
<Bean id = "dateProperty" class = "com. zhmg. spring. DatePropertyInjection">
<Property name = "date">
<Value> 2009-8-28 </value>
</Property>
</Bean>
</Beans>

ApplicationContextBeans. xmlCopy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: aop = "http://www.springframework.org/schema/aop"
Xmlns: tx = "http://www.springframework.org/schema/tx"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd ">
<! --
<Bean id = "dateProperty" class = "com. zhmg. spring. DatePropertyInjection">
<Property name = "date">
<Value> 2009-8-28 </value>
</Property>
</Bean>
-->
<Bean id = "editor" class = "org. springframework. beans. factory. config. CustomEditorConfigurer">
<Property name = "customEditors">
<Map>
<Entry key = "java. util. Date">
<Bean class = "com. zhmg. spring. PropertyEditor">
<! -Inject date format -->
<Property name = "format" value = "yyyy-MM-dd"/>
</Bean>
</Entry>
</Map>
</Property>
</Bean>
</Beans>

Unit: InjectionTest. javaCopy codeThe Code is as follows: package com. zhmg. spring;
Import junit. framework. TestCase;
Import org. springframework. beans. factory. BeanFactory;
Import org. springframework. context. support. ClassPathXmlApplicationContext;
Public class InjectionTest extends TestCase {
BeanFactory factory;
Protected void setUp () throws Exception {
// Read all configuration files starting with applicationContext using wildcards
Factory = new ClassPathXmlApplicationContext ("applicationContext *. xml ");
}
Public void testInjection (){
DatePropertyInjection dateProp = (DatePropertyInjection) factory. getBean ("dateProperty ");
System. out. println ("date =" + dateProp. getDate ());
}
}

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.