通过Setters方式对日期属性及日期格式进行IOC注入
程序员文章站
2024-02-16 22:52:46
本实例中还涉及到spring中采用多个配置文件,也涉及到对日期格式的注入-------更加灵活 date属性类: datepropertyinjection.java 复制...
本实例中还涉及到spring中采用多个配置文件,也涉及到对日期格式的注入-------更加灵活
date属性类: datepropertyinjection.java
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;
}
}
属性编辑器类:propertyeditor.java
package com.zhmg.spring;
import java.beans.propertyeditorsupport;
import java.text.parseexception;
import java.text.simpledateformat;
import java.util.date;
/**
* 自定义属性编辑器处理java.util.date类型的属性
* @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
<?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
<?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">
<!—对日期格式注入-->
<property name="format" value="yyyy-mm-dd"/>
</bean>
</entry>
</map>
</property>
</bean>
</beans>
测试单元:injectiontest.java
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 {
//采用通配符的方式读取所有以applicationcontext开头的配置文件
factory = new classpathxmlapplicationcontext("applicationcontext*.xml");
}
public void testinjection(){
datepropertyinjection dateprop = (datepropertyinjection)factory.getbean("dateproperty");
system.out.println("date=" + dateprop.getdate());
}
}
date属性类: datepropertyinjection.java
复制代码 代码如下:
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;
}
}
属性编辑器类:propertyeditor.java
复制代码 代码如下:
package com.zhmg.spring;
import java.beans.propertyeditorsupport;
import java.text.parseexception;
import java.text.simpledateformat;
import java.util.date;
/**
* 自定义属性编辑器处理java.util.date类型的属性
* @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
复制代码 代码如下:
<?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
复制代码 代码如下:
<?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">
<!—对日期格式注入-->
<property name="format" value="yyyy-mm-dd"/>
</bean>
</entry>
</map>
</property>
</bean>
</beans>
测试单元:injectiontest.java
复制代码 代码如下:
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 {
//采用通配符的方式读取所有以applicationcontext开头的配置文件
factory = new classpathxmlapplicationcontext("applicationcontext*.xml");
}
public void testinjection(){
datepropertyinjection dateprop = (datepropertyinjection)factory.getbean("dateproperty");
system.out.println("date=" + dateprop.getdate());
}
}