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 ());
}
}