An IOC injection _JSP programming of date attributes and date formats by Setters method

Source: Internet
Author: User
Tags aop dateformat getdate string format xmlns
This example also involves the adoption of multiple profiles in spring, as well as the injection of date formats-------more flexible
Date Attribute class: Datepropertyinjection.java
Copy Code code 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;
}
}

Property Editor Class: Propertyeditor.java
Copy Code code as follows:

Package com.zhmg.spring;
Import Java.beans.PropertyEditorSupport;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
/**
* Custom Property Editor handles properties of 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.xml
Copy code code 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.xml
Copy Code code 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" >
<!-the date format into-->
<property name= "format" value= "Yyyy-mm-dd"/>
</bean>
</entry>
</map>
</property>
</bean>
</beans>

Test Unit: Injectiontest.java
Copy Code code 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 {
///wildcard to read all configuration files that start with ApplicationContext
Factory = new ClassPath Xmlapplicationcontext ("Applicationcontext*.xml");
}
public void Testinjection () {
Datepropertyinjection Dateprop = (datepropertyinjection) Factory.getbean ("D Ateproperty ");
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.