spring配置自定义的PropertyEditor

在项目开发过程中,总会有这样那样由于格式转换带来的问题。如:money精确度的处理,这个应该在电商中非常常见。日期格式的处理,几乎是所有开发过程遇到的。本文通过介绍java.beans.PropertyEditor
复习一下。

PropertyEditor
、PropertyEditorSupport

在上层是一个Property的简单接口,仅仅定义了setValue
setAsText等几个非常简单的接口。后面通过PropertyEditorSupport来做了一些实现。最常用的方法也就是setAsText setValue
分别是用来转换字符串,以及注入转换后的值。

DataUtil

package cfl.spring.beans;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DateUtil extends PropertyEditorSupport {

	private String pattern;

	public String getPattern() {
		return pattern;
	}

	public void setPattern(String pattern) {
		this.pattern = pattern;
	}

	@Override
	public void setAsText(String text) throws IllegalArgumentException {

		try {

		    SimpleDateFormat df=new SimpleDateFormat(pattern ,Locale.CHINA);
		    Date date=df.parse(text);
			this.setValue(date);
			System.out.println(date);

		} catch (ParseException e) {

			e.printStackTrace();

		}
	}

}

自定义格式处理的字符串的类。通过重写父类的方法来根据项目开发的需求,自定义格式处理字符串。

同时将需要配置为何种格式的日期类型,通过spring注入。利于手动灵活配置。

配置到spring中自动处理

<?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.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="customer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
		<property name="customEditors">
			<map>
				<entry key="java.util.Date">
					<bean id="dateUtil" class="cfl.spring.beans.DateUtil">
						<property name="pattern">
							<value>yyyy-MM-dd</value>
						</property>
					</bean>
				</entry>
			</map>
		</property>
	</bean>
</beans>

贴上测试类吧,方便阅读

package cfl.test.configure;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cfl.spring.beans.Bean1;
import junit.framework.TestCase;

public class configureTest extends TestCase{
	private BeanFactory beanfactory;
	protected void setUp() throws Exception {

		//方式1:读取单个xml文件
		//beanfactory=new ClassPathXmlApplicationContext("applicationContext.xml");
		//方式2:读取多个xml文件
		String[] strings=new String[]{"applicationContext-basic.xml","applicationContext-edit.xml"};
		//beanfactory=new ClassPathXmlApplicationContext(strings);
		//方式3:读取指定规格的xml文件
		beanfactory =new ClassPathXmlApplicationContext(strings);
	}
	public void testConfigure(){

		Bean1 bean1=(Bean1)beanfactory.getBean("Bean1");
		System.out.println(bean1.getBean2().getAge());
		System.out.println(bean1.getBean3().getValueList());
		System.out.println(bean1.getBean3().getDataValue());
	}
}
时间: 2024-08-30 16:53:58

spring配置自定义的PropertyEditor的相关文章

Spring Boot自定义Redis缓存配置,保存value格式JSON字符串

Spring Boot自定义Redis缓存,保存格式JSON字符串 部分内容转自 https://blog.csdn.net/caojidasabi/article/details/83059642 package springboot01cache.config; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; im

spring mvc 自定义编辑器

起始知识: Java标准的PropertyEditor的核心功能是将一个字符串转换为一个Java对象,以便根据界面的输入或配置文件中的配置字符串构造出一个JVM内部的java对象. 如何注册自定义的属性编辑器: 1.实现PropertyEditor接口或者继承PropertyEditorSupport类 2.在Spring上下文中声明一个org.springframework.beans.factory.config.CustomEditorConfigurer的bean <!--将bean1中

使用Spring处理自定义注解

使用Spring处理自定义注解 本文只讲思想,不讲代码. 可能的两种方法 spring schema spring aop aspect 参考1 dubbo service 包名:com.alibaba.dubbo.config 参考2 spring mvc 包名:org.springframework.web.servlet.config 可以参考这两个的实现,利用schema添加自定义注解并处理自己的注解,注册搜索模块. 源码分析 通过schema添加配置解析如: 在 spring配置文件中

spring AOP + 自定义注解实现权限控制小例子

今天看了一下黑马程序员的视频,上面讲到一个使用spring AOP + 自定义注解的方式来实现权限控制的一个小例子,个人觉得还是可以借鉴,整理出来与大家分享. 需求:service层有一些方法,这些方法需要不同的权限才能访问. 实现方案:自定义一个PrivilegeInfo的注解,使用这个注解为service层中的方法进行权限配置,在aop中根据PrivilegeInfo注解的值,判断用户是否拥有访问目标方法的权限,有则访问目标方法,没有则给出提示. 关键技术:自定义注解及注解解析,spring

Spring aop +自定义annotation

Spring aop +自定义注解 一.所需的jar 包: <!-- 导入java ee jar 包 --> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> </dependency> <!--spring ,springmvc--> <

Spring MVC自定义统一异常处理类,并且在控制台中输出错误日志

在使用SimpleMappingExceptionResolver实现统一异常处理后(参考Spring MVC的异常统一处理方法), 发现出现异常时,log4j无法在控制台输出错误日志.因此需要自定义一个继承至SimpleMappingExceptionResolver的 RrtongMappingExceptionResolver类,在RrtongMappingExceptionResolver中通过 log.error(ex.getMessage())的方式输出日志到控制台上.以下是具体的配

spring AOP自定义注解方式实现日志管理

转:spring AOP自定义注解方式实现日志管理 今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在applicationContext-mvc.xml中要添加的 <mvc:annotation-driven />     <!-- 激活组件扫描功能,在包com.gcx及其子包下面自动扫描通过注解配置的组件 -->     <conte

Dubbo中对Spring配置标签扩展

Spring提供了可扩展Schema的支持,完成一个自定义配置一般需要以下步骤: 设计配置属性和JavaBean 编写XSD文件 编写NamespaceHandler和BeanDefinitionParser完成解析工作 编写spring.handlers和spring.schemas串联起所有部件 在Bean文件中应用 dubbo中所有dubbo的标签,都统一用DubboBeanDefinitionParser进行解析,基于一对一属性映射,将XML标签解析为Bean对象. 下面我们就用dubb

(转)利用Spring AOP自定义注解解决日志和签名校验

一.需解决的问题 部分API有签名参数(signature),Passport首先对签名进行校验,校验通过才会执行实现方法. 第一种实现方式(Origin):在需要签名校验的接口里写校验的代码,例如: boolean isValid = accountService.validSignature(appid, signature, client_signature); if (!isValid) return ErrorUtil.buildError(ErrorUtil.ERR_CODE_COM