1. 通过Setters方式对日期属性及日期格式进行IOC注入
  1. Spring不直接支持对Date类型的IOC依赖关系的注入,而提供了类似于Struts中的converte一样的属性编辑器,需要程序员自己写相应的类,并将其注册。
  2. 本实例中还涉及到Spring中采用多个配置文件,也涉及到对日期格式的注入-------更加灵活
  3. Date属性类: DatePropertyInjection.java
  1. package com.zhmg.spring;
  2. import java.util.Date;
  3. public class DatePropertyInjection {
  4. private Date date;
  5. public Date getDate() {
  6. return date;
  7. }
  8. public void setDate(Date date) {
  9. this.date = date;
  10. }
  11. }
  1. 属性编辑器类:PropertyEditor.java
  1. package com.zhmg.spring;
  2. import java.beans.PropertyEditorSupport;
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6. /**
  7. * 自定义属性编辑器处理java.util.Date类型的属性
  8. * @author Administrator
  9. *
  10. */
  11. public class PropertyEditor extends PropertyEditorSupport {
  12. String format = "yyyy-MM-dd";
  13. public void setFormat(String format) {
  14. this.format = format;
  15. }
  16. @Override
  17. public void setAsText(String arg0) throws IllegalArgumentException {
  18. SimpleDateFormat dateFormat = new SimpleDateFormat(format);
  19. try {
  20. Date date = dateFormat.parse(arg0);
  21. this.setValue(date);
  22. } catch (ParseException e) {
  23. // TODO Auto-generated catch block
  24. e.printStackTrace();
  25. }
  26. }
  27. }
  1. applicationContextBeans.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
  8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
  9. <bean id="dateProperty" class="com.zhmg.spring.DatePropertyInjection">
  10. <property name="date">
  11. <value>2009-8-28</value>
  12. </property>
  13. </bean>
  14. </beans>
  1. applicationContextBeans.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
  8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
  9. <!--
  10. <bean id="dateProperty" class="com.zhmg.spring.DatePropertyInjection">
  11. <property name="date">
  12. <value>2009-8-28</value>
  13. </property>
  14. </bean>
  15. -->
  16. <bean id="editor" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
  17. <property name="customEditors">
  18. <map>
  19. <entry key="java.util.Date">
  20. <bean class="com.zhmg.spring.PropertyEditor">
  21. <!—对日期格式注入-->
  22. <property name="format" value="yyyy-MM-dd"/>
  23. </bean>
  24. </entry>
  25. </map>
  26. </property>
  27. </bean>
  28. </beans>
  1. 测试单元:InjectionTest.java
  1. package com.zhmg.spring;
  2. import junit.framework.TestCase;
  3. import org.springframework.beans.factory.BeanFactory;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. public class InjectionTest extends TestCase {
  6. BeanFactory factory;
  7. protected void setUp() throws Exception {
  8. //采用通配符的方式读取所有以applicationContext开头的配置文件
  9. factory = new ClassPathXmlApplicationContext("applicationContext*.xml");
  10. }
  11. public void testInjection(){
  12. DatePropertyInjection dateProp = (DatePropertyInjection)factory.getBean("dateProperty");
  13. System.out.println("date=" + dateProp.getDate());
  14. }
  15. }

更多相关文章

  1. 设置android默认属性
  2. API 25 (Android(安卓)7.1.1 API) view.View——属性分析
  3. android 软键盘回车键捕获
  4. AndroidManifest.xml文件详解(uses-configuration)
  5. RelativeLayout常用属性介绍
  6. 通过Setters方式对日期属性及日期格式进行IOC注入
  7. 通过Setters方式对日期属性及日期格式进行IOC注入
  8. 实例演示函数参数与返回值、演示模板字面量与模板函数
  9. 访问器属性原理与应用场景,获取 dom 元素的两个重要方法

随机推荐

  1. Android使用SVG矢量图打造酷炫动画效果
  2. Android中获取网络天气数据
  3. android调用系统通讯录,并返回联系人号码
  4. Android开发四大组件之Service(实例篇)
  5. android Room框架学习
  6. 内存探究记录
  7. Android Service的生命周期图解
  8. react native 0.49 android版本热更新
  9. 如何在android地图中使用TextWatcher显示
  10. 如何从firebase中获取唯一ID内的数据