详解spring自定义属性编辑器

Spring DI注入的时候可以把普通属性注入进来,但是像Date类型的就无法被识别。这时可以通过Spring的属性编辑器把配置文件中的字符串转化成相应的对象进行注入。

Spring有自带的属性编辑器,我们也可以写自定义的属性编辑器

自定义属性编辑器:

继承java.beans.PropertyEditorSupport类,重写其中的setAsText(String text)方法。

再把自定义的属性编辑器注入到Spring中。

1. JavaBean类

package com.dxc.zidingyishuxingbianjiqi;

import java.util.Date;

public class DateBean {

private Date dataValue;

public Date getDataValue() {

return dataValue;

}

public void setDataValue(Date dataValue) {

this.dataValue = dataValue;

}

}

2.自定义属性编辑器:

package com.dxc.zidingyishuxingbianjiqi;

import java.beans.PropertyEditorSupport;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

/**

* java.util.Date属性编辑器

*/

public class UtilDatePropertyEditor extends PropertyEditorSupport {

private String format="yyyy-MM-dd";

@Override

public void setAsText(String text) throws IllegalArgumentException {

// TODO Auto-generated method stub

SimpleDateFormat sdf=new SimpleDateFormat(format);

try {

Date date=sdf.parse(text);

this.setValue(date);

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public void setFormat(String format){

this.format=format;

}

}

3. 配置自定义的属性编译器

<?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:p="http://www.springframework.org/schema/p"

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<!-- 将bean中的dataValue赋值为2008-8-15,spring会认为是String类型,无法转换为Date -->

<bean id="dateBean" class="com.dxc.zidingyishuxingbianjiqi.DateBean">

<property name="dataValue">

<value>2008-08-15</value>

</property>

</bean>

<!-- 自定义属性编辑器 -->

<bean id="customerEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">

<property name="customEditors">

<map>

<entry key="java.util.Date">

<bean class="com.dxc.zidingyishuxingbianjiqi.UtilDatePropertyEditor">

<property name="format">

<value>yyyy-MM-dd</value>

</property>

</bean>

</entry>

</map>

</property>

</bean>

</beans>

3.测试结果,

package com.dxc.zidingyishuxingbianjiqi;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestDate {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

ApplicationContext ac=new ClassPathXmlApplicationContext("com/dxc/zidingyishuxingbianjiqi/applicationContext.xml");

DateBean bean=(DateBean) ac.getBean("dateBean");

System.out.println(bean.getDataValue());

}

}

注释掉自定义编辑器,会报错,把String转换Date错误。

时间: 2024-08-28 04:52:18

详解spring自定义属性编辑器的相关文章

实例详解Spring的事务传播机制(二)

上面我们讨论了NEVER和MANDATORY的作用,下面我们接着讨论其他情况. 3. SUPPORTS 如果有事务则加入该事务,如果没有存在的事务则以非事务的方式运行. 我们先让insertSubTable方法在无事务的情况下运行.配置文件为: <tx:attributes>       <!--     <tx:method name="insertSuperTable" propagation="REQUIRED"/>      -

详解spring 每个jar的作用

详解spring 每个jar的作用: spring.jar 是包含有完整发布模块的单个jar 包.但是不包括mock.jar, aspects.jar, spring-portlet.jar, and spring-hibernate2.jar.spring-src.zip就是所有的源代码压缩包.除了spring.jar 文件,Spring 还包括有其它21 个独立的jar 包,各自包含着对应的Spring组件,用户可以根据自己的需要来选择组合自己的jar 包,而不必引入整个spring.jar

详解Spring框架的核心思想之IOC

微信号:GitShare微信公众号:爱折腾的稻草如有问题或建议,请在公众号留言[1] 前续 为帮助广大SpringBoot用户达到"知其然,更需知其所以然"的境界,作者将通过SpringBoot系列文章全方位对SpringBoot2.0.0.RELEASE版本深入分解剖析,让您深刻的理解其内部工作原理. No.1 Spring是什么 为了让更多的朋友了解Spring,首先科普一下Spring!有兴趣的朋友可以去Spring官网逛逛,地址是:https://spring.io/ The

用IDEA详解Spring中的IoC和DI(挺透彻的,点进来看看吧)

用IDEA详解Spring中的IoC和DI 一.Spring IoC的基本概念 控制反转(IoC)是一个比较抽象的概念,它主要用来消减计算机程序的耦合问题,是Spring框架的核心.依赖注入(DI)是IoC的另外一种说法,只是从不同的角度描述相同的概念.看完这两句,是不是不但没懂,反而更迷惑了,别急,往下看: IoC的背景 我们都知道,在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最终实现系统的业务逻辑. 如果我们打开机械式手表的后盖,就会看到与

[转载] 多图详解Spring框架的设计理念与设计模式

转载自http://developer.51cto.com/art/201006/205212_all.htm Spring作为现在最优秀的框架之一,已被广泛的使用,51CTO也曾经针对Spring框架中的JDBC应用做过报道.本文将从另外一个视角试图剖析出Spring框架的作者设计Spring框架的骨骼架构的设计理念. AD: Spring作为现在最优秀的框架之一,已被广泛的使用,51CTO也曾经针对Spring框架中的JDBC应用做过报道.本文将从另外一个视角试图剖析出Spring框架的作者

RabbitMQ实例详解+Spring中的MQ使用

RabbitMQ实例详解 消息队列中间件是分布式系统中重要的组件,主要解决应用解耦,异步消息,流量削锋等问题,实现高性能,高可用,可伸缩和最终一致性架构. Queue Queue(队列)是RabbitMQ的内部对象,用于存储消息,用下图表示. RabbitMQ中的消息都只能存储在Queue中,生产者(下图中的P)生产消息并最终投递到Queue中,消费者(下图中的C)可以从Queue中获取消息并消费. 多个消费者可以订阅同一个Queue,这时Queue中的消息会被平均分摊给多个消费者进行处理,而不

Spring自定义属性编辑器

bean类: [html] view plain copy package com.zm.bean; import java.util.Date; public class Bean1 { private Date dateValue; public Date getDateValue() { return dateValue; } public void setDateValue(Date dateValue) { this.dateValue = dateValue; } } 自定义属性编辑

Spring Boot? 配置文件详解:自定义属性、随机数、多环境配置等

自定义属性与加载 我们在使用Spring Boot的时候,通常也需要定义一些自己使用的属性,我们可以如下方式直接定义: application-dev.yml com.didispace.blog: name: 程序猿DD title: Spring Boot教程 desc: ${com.didispace.blog.name}正在努力写<${com.didispace.blog.title}> # 随机字符串 value: ${random.value} # 随机int number: ${

详解 Spring 3.0 基于 Annotation 的依赖注入实现(转)

使用 @Repository.@Service.@Controller 和 @Component 将类标识为 Bean Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的开发.@Repository 注解便属于最先引入的一批,它用于将数据访问层 (DAO 层 ) 的类标识为 Spring Bean.具体只需将该注解标注在 DAO 类上即可.同时,为了让 Spring 能够扫描类路径中的类并识别出 @Repository 注解,需要在 XML 配置文件中启用 Bean