Spring中的属性编辑器的使用

转载自 http://blog.sina.com.cn/s/blog_59ca2c2a0100jxwh.html


 Struts中用一个类型转换器,在Spring中也有类似的功能,叫做属
性编辑器,用于将spring配置文件中配置的字符串转换为Action中相应的类型。

这里有一个普通的JavaBean,代码如下:

package cn.com.huixin.spring;
import java.util.Date;
import java.util.List;
import java.util.Map;
public class Bean1 {
private String stringValue;
private List list1;
private Map map1;
private String[] array;
private Date birthday;
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getStringValue() {
return stringValue;
}
public void setStringValue(String stringValue) {
this.stringValue = stringValue;
}
public List getList1() {
return list1;
}
public void setList1(List list1) {
this.list1 = list1;
}
public Map getMap1() {
return map1;
}
public void setMap1(Map map1) {
this.map1 = map1;
}
public String[] getArray() {
return array;
}
public void setArray(String[] array) {
this.array = array;
}
}
  里边有几个属性需要使用spring注入。前边的四个属性注入都没有 问题,只有后边的Date型的属性注入时会出错,提示类型不匹配。从
spring配置文件中出过来的都是String型的字符串值,而Bean中是
一个Date类型的值,这个属性就需要我们提供一个属性编辑器来完成类型转换工作。
  • 首先创建一个属性编辑器,该类需要继承

    java.beans.PropertyEditorSupport类,代码如下所示:

package cn.com.huixin.editor;
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DatePropertyEditor extends PropertyEditorSupport {
private String format = "yyyy-MM-dd";
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
Date date = null;
try {
date = dateFormat.parse(text);
} catch (ParseException e) {
e.printStackTrace();
}
this.setValue(date);
}
}
该类中有一个String型的format属性,为日期格式,并为它提供的
法用于进行类型转换。参数text为从spring配置文件中获得的字符串值。使用SimpleDateFormat对象将其转换为Date对象后,调用
setValue()方法将转换后的对象放入value对象中。
接下来配置applicationContext.xml文件,代码如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Application context definition for JPetStore's business layer. -
Contains bean references to the transaction manager and to the DAOs in
- dataAccessContext-local/jta.xml (see web.xml's
"contextConfigLocation").
-->
<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.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="bean1" class="cn.com.huixin.spring.Bean1">
<property name="stringValue" value="stringValue1" />
<property name="list1">
<list>
<value>listValue1</value>
<value>listValue2</value>
<value>listValue3</value>
</list>
</property>
<property name="map1">
<map>
<entry key="mapkey1" value="mapValue1" />
<entry key="mapkey2" value="mapValue2" />
</map>
</property>
<property name="array">
<list>
<value>arrayValue1</value>
<value>arrayValue2</value>
</list>
</property>
<property name="birthday" value="2010-07-24"/>
</bean>
</beans>
 这个文件中对Bean1种的各属性注入了值。接下来再提供一个
applicationContext-propertyEditor.xml文件,专门用于属性编辑器的配置,代码如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Application context definition for JPetStore's business layer.
- Contains bean references to the transaction manager and to the DAOs in
- dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
-->
<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.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class="cn.com.huixin.editor.DatePropertyEditor">
<property name="format" value="yyyy-MM-dd"/>
</bean>
</entry>
</map>
</property>
</bean>
</beans>
 该文件中实例化了一个  org.springframework.beans.factory.config.CustomEditorCon
figurer的对象,该类中有一个名为customEditors的Map对象,
在该对象中添加了一个键值对,key属性值为java.util.Date,即
类型转换的目标类型,value属性值为
cn.com.huixin.editor.DatePropertyEditor,即我们编写的
属性编辑器,这个类用来进行类型转换。在
cn.com.huixin.editor.DatePropertyEditor类中有一个名为
format的属性值,即日期格式,这里我们也对它进行了注入。到目前
为止,我们的属性编辑器就编写好了,下来写一个类进行测试。测试
代码如下所示:
package cn.com.huixin.test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.com.huixin.spring.Bean1;
import junit.framework.TestCase;
public class BeanInject extends TestCase {
private BeanFactory factory;
@Override
protected void setUp() throws Exception {
factory = new ClassPathXmlApplicationContext("applicationContext*.xml");
}
public void testBean() {
Bean1 bean1 = (Bean1) factory.getBean("bean1");
System.out.println(bean1.getStringValue());
System.out.println(bean1.getList1());
System.out.println(bean1.getMap1());
System.out.println(bean1.getArray());
System.out.println(bean1.getBirthday());
}
}
首先通过ClassPathXmlApplicationContext对象加载了以
applicationContext开头的xml文件,获得一个BeanFactory对
象,然后调用该对象的getBean()方法就可以获得为属性注入的值。
运行的结果为:
stringValue1
[listValue1, listValue2, listValue3]
{mapkey1=mapValue1, mapkey2=mapValue2}
[Ljava.lang.String;@1982fc1
Sat Jul 24 00:00:00 CST 2010

说明我们的值注入成功。

Spring中的属性编辑器的使用的更多相关文章

  1. Spring中使用属性文件properties的两种方式

    实际项目中,通常将可配置的参数放到属性文件中,例如数据库连接信息.redis连接信息等,便于统一管理.然后通过IoC框架spring将其加载到上下文中,使得程序可以直接使用. 创建mysql.prop ...

  2. 这篇文章,我们来谈一谈Spring中的属性注入

    本系列文章: 读源码,我们可以从第一行读起 你知道Spring是怎么解析配置类的吗? 配置类为什么要添加@Configuration注解? 谈谈Spring中的对象跟Bean,你知道Spring怎么创 ...

  3. Spring中的属性注入注解

    @Inject使用 JSR330规范实现的 默认按照类型注入 如果需要按照名称注入,@Inject需要和@Name一起使用 @Resource JSR250规范实现的,需要导入不同的包 @Resour ...

  4. spring中ref属性与<ref/>标签

    在bean的内部引用另一个bean对象: 使用ref标签对其进行引用: <ref bean="viewResolver2"/> <bean id="vi ...

  5. [置顶] Spring中自定义属性编辑器

    Spring中的属性编辑器能够自动的将String类型转化成需要的类型,例如一个类里面的一个整型属性,在配置文件中我们是通过String类型的数字进行配置的,这个过程中就需要一个转化操作,当然这个转化 ...

  6. 属性编辑器,即PropertyEditor-->Spring IoC

    在Spring配置文件里,我们往往通过字面值为Bean各种类型的属性提供设置值:不管是double类型还是int类型,在配置文件中都对应字符串类型的字面值.BeanWrapper填充Bean属性时如何 ...

  7. 【Spring源码解读】bean标签中的属性

    说明 今天在阅读Spring源码的时候,发现在加载xml中的bean时,解析了很多标签,其中有常用的如:scope.autowire.lazy-init.init-method.destroy-met ...

  8. (spring-第13回【IoC基础篇】)PropertyEditor(属性编辑器)--实例化Bean的第五大利器

    上一篇讲到JavaBeans的属性编辑器,编写自己的属性编辑器,需要继承PropertyEditorSupport,编写自己的BeanInfo,需要继承SimpleBeanInfo,然后在BeanIn ...

  9. 谈谈Spring中的对象跟Bean,你知道Spring怎么创建对象的吗?

    本系列文章: 读源码,我们可以从第一行读起 你知道Spring是怎么解析配置类的吗? 配置类为什么要添加@Configuration注解? 推荐阅读: Spring官网阅读 | 总结篇 Spring杂 ...

随机推荐

  1. 个人永久性免费-Excel催化剂功能第31波-数量金额分组凑数功能,财务表哥表姐最爱

    在财务工作过程中,很大时候需要使用到凑数的需求,花了两三天时间认真研究了一下,本人水平也只能做代码搬运工,在用户体验上作了一下完善.完成了Excel版的凑数功能. 文章出处说明 原文在简书上发表,再同 ...

  2. UVA663 Sorting Slides(烦人的幻灯片)

    UVA663 Sorting Slides(烦人的幻灯片) 第一次做到这么玄学的题,在<信息学奥赛一本通>拓扑排序一章找到这个习题(却发现标程都是错的),结果用二分图匹配做了出来 蒟蒻感觉 ...

  3. Spark学习之RDD

    RDD概述 什么是RDD RDD(Resilient Distributed Dataset)叫做弹性分布式数据集,是Spark中最基本的数据抽象,它代表一个不可变.可分区.里面的元素可并行计算的集合 ...

  4. Word公式显示为{EMBED Equation.DSMT4}

    具体问题表现为: 添加了Mathtype公式后显示为{EMBED Equation.DSMT4}, 超链接显示为大花括号和描述文本, 页码显示为​ page... 具体解决方法如下,(以Office2 ...

  5. 《VR入门系列教程》之8---GearVR

    高端移动虚拟现实设备---三星GearVR     Oculus Rift也许是虚拟现实头显的典范,但是它还是存在许多问题.首先,它需要基于一个具有强大图形计算能力的计算机,而使用一般的笔记本.苹果A ...

  6. jsp定义全局变量:读取properties文件

    <%java.util.Properties prop = new java.util.Properties();java.io.InputStream in;in = getClass().g ...

  7. 阿里云nas使用记录

    公司买了阿里云的nas服务用来共享存储,多个web服务器共同挂载同一个nas服务.挂载过程中出现如下报错 NAS报错: [root@BJ-SBC fs]# mount -t nfs 10.10.8.1 ...

  8. 对ThreadLocal的一些理解

    ThreadLocal也是在面试过程中经常被问到的,本文主要从以下三个方面来谈对ThreadLocal的一些理解: ThreadLocal用在什么地方 ThreadLocal一些细节 ThreadLo ...

  9. Android开发进阶——自定义View的使用及其原理探索

    在Android开发中,系统提供给我们的UI控件是有限的,当我们需要使用一些特殊的控件的时候,只靠系统提供的控件,可能无法达到我们想要的效果,这时,就需要我们自定义一些控件,来完成我们想要的效果了.下 ...

  10. Selenium+java - 弹出框处理

    一.弹出框分类: 弹出框分为两种,一种基于原生JavaScript写出来的弹窗,另一种是自定义封装好的样式的弹出框,本文重点介绍原生JavaScript写出来的弹窗,另一种弹窗用click()基本就能 ...