Spring IOC 自定义属性编辑器PropertyEditor

Spring中我们可以使用属性编辑器来将特定的字符串转换为对象 :String-->object

 java.beans.PropertyEditor(JDK中的接口)用于将xml文件中字符串转换为特定的类型
同时JDK为我们提供一个实现类java.beans.PropertyEditorSupport
Spring在注入时,如果遇到类型不一致(例如需要Address类型但是用户传了个String)则会去调用相应的属性编辑器进行转换.
Spring会调用属性编辑器的setAsText(String str)进行处理用户传的字符串,并调用getValue()方法获取处理后得到的对象,所以我们在代码中处理完后记得调用setValue方法,要不然spring调用getValue方法拿不到你处理后的对象

自定义属性编辑器示例:

注意:spring中的CustomEditorConfigurer类的使用,在htmlsingle中直接搜索类名即可

自定义编辑器类AddressEditor:

public class AddressEditor extends PropertyEditorSupport{

	//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);
		//最后这一步一定要写--》return add;
		setValue(add);
	}
}
Address类--表示地址的Bean类

public class Address {

	private String city;
	private String street;
	private String country;
	
	public Address(){
		
	}
	public Address(String city, String street, String country) {
		this.city = city;
		this.street = street;
		this.country = country;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getStreet() {
		return street;
	}
	public void setStreet(String street) {
		this.street = street;
	}
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
	
	public String toString(){
		return "city="+city+" street="+street+" country="+country;
	}
}
Student类:

public class Student {

	private long id;
	private String name;
	private boolean gender;
	private int age;
	private Address address;
	
	
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public boolean isGender() {
		return gender;
	}
	public void setGender(boolean gender) {
		this.gender = gender;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	public String toString(){
		return "id="+id+" name="+name+" gender="+gender+" age="+age+
		" address:"+address;
	}
}

这个类有Address对象作为属性~这代表每个人都有一个地址~

配置文件plugin.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
	  <!-- 这个配置指明哪个类型对应哪个自定义编辑器 -->
	<bean 
	class="org.springframework.beans.factory.config.CustomEditorConfigurer">
		<property name="customEditors">
			<map>
      			<entry key="com.x.spring.test3.proEdit.Address" value="com.x.spring.test3.proEdit.AddressEditor"/>
    		</map>
		</property>
	</bean>
</beans>
proEdit.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
	
	<!-- spring发现address的值不能注入的时候(类型不对),就会调用对应的属性编辑器处理了 -->
	<bean id="student" class="com.x.spring.test3.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.rd,China</value>
		</property>
	</bean>
</beans>
测试类:

public class EditTest {

	public static void main(String[] args) {
		//自定义属性编辑器 PropertyEditor
		try {
			String[] path = {"com/x/spring/test3/proEdit/plugin.xml","com/x/spring/test3/proEdit/proEdit.xml"};
			ApplicationContext container = 
					new ClassPathXmlApplicationContext(path);
			Student s = (Student)container.getBean("student");
			System.out.println(s);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
运行效果:




可能理解困难,多看几遍就好了~


      

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值