spring学习教程8-自定义属性编辑器PropertyEditor 和自定义事件

本文介绍Spring框架中自定义属性编辑器PropertyEditor的应用,用于解决类型转换问题,并演示如何通过CustomEditorConfigurer配置自定义编辑器。此外,还介绍了自定义事件的功能及其在Spring中的实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自定义属性编辑器PropertyEditor 和自定义事件

一.自定义属性编辑器PropertyEditor 
Spring中我们可以使用属性编辑器来将特定的字符串转换为对象。
String--转换-->object
java.beans.PropertyEditor(JDK中的接口)用于将xml文件中字符串转换为特定的类型,同时JDK为我们提供一个实现类java.beans.PropertyEditorSupport。
Spring在注入时,如果遇到类型不一致(例如需要Address类型但是用户传了个String)则会去调用相应的属性编辑器进行转换。
spring会调用属性编辑器的setAsText(String str)进行处理用户传的字符串,并调用getValue()方法获取处理后得到的对象
在代码中处理完后需要调用setValue方法,要不然spring调用getValue方法拿不到处理后转换成的对象
自定义属性编辑器示例:
注意:在配置文件中CustomEditorConfigurer类的使用,在htmlsingle中直接搜索类名即可

//自定义编辑器类
    public class AddressEditor extends PropertyEditorSupport {
		@Override
		public String getAsText() {
			return super.getAsText();
		}
		
		//Spring遇到数据类型不一致并且不能自己处理的时候会调用这个方法处理字符串
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			String[] str = text.split(",");
			String city = str[0];
			String street = str[1];
			String country = str[2];
			Address add = new Address(city, street, country);
			setValue(add);
		}


    }


    //Address类
    public class Address {
		private String city;
		private String street;
		private String country;
		set/get
		.....
    }


    //Student类
    public class Student {
		private long id;
		private String name;
		private boolean gender;
		private int age;
		private Address address;
		get/set
		...
    }
    xmlspring配置文件:
<!-- 这个配置指明哪个类型对应哪个自定义编辑器 -->
		<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
			<property name="customEditors">
				<map>
					<entry key="com.briup.ioc.proEdit.Address"			                 value="com.briup.ioc.proEdit.AddressEditor"/>
				</map>
			</property>
		</bean>
      
	<!-- spring发现address的类型是Address的时候,就会调用对应的属性编辑器处理AddressEditor了 -->
		<bean id="student" class="com.briup.ioc.proEdit.Student">
			<property name="id" value="1"/>
			<property name="name" value="tom"/>
			<property name="age" value="45"/>
			<property name="gender" value="true"/>
			<property name="address">
				<value>kunshan,xueyuan,China</value>
			</property>
		</bean>
二.自定义事件

在spring中我们可以自定义事件,并且可以使用ApplicationContext类型对象(就是spring容器container)来发布这个事件
事件发布之后,所有的ApplicaitonListener(监听器)实例都会被触发并调用指定方法onApplicationEvent()来处理.
  
例如:

自定义事件类RainEvent:
	public class RainEvent extends ApplicationEvent {
		public RainEvent(Object source) {
			super(source);
		}
	}
  
	监听器类RainListener1
	public class RainListener1 implements ApplicationListener<RainEvent>{


		@Override
		public void onApplicationEvent(RainEvent event) {
			System.out.println("唐僧大喊:" + event.getSource() + "赶快收衣服喽!");
		}


	}


	监听器类RainListener2
	public class RainListener2 implements ApplicationListener<RainEvent>{


		public void onApplicationEvent(RainEvent event) {
			System.out.println("我们:" + event.getSource() + "太好了不用上课了!");
		}
	}
xml文件:
<!-- 只需要把这俩个监听器类交给spring容器管理就可以了 -->
	<bean class="com.briup.ioc.event.RainListener1"></bean>
	<bean class="com.briup.ioc.event.RainListener2"></bean>
main:
String path = "com/briup/ioc/event/event.xml";
ApplicationContext container = 
new ClassPathXmlApplicationContext(path);
container.publishEvent(new RainEvent("下雨了!"));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值