Spring自定义属性编辑器

自定义Spring属性编辑器
流程:1,编写被解析类和解析类Address,Customer;
2,定义一个属性编辑器AddressPropertyEditor,继承于PropertyEditorSupport类,重写里面setAsText()方法;
3,定义一个属性编辑器注册器AddressPropertyEditorRegistrar,继承于PropertyEditorRegistrar类,重写里面registerCustomEditors()方法;
4,编写配置文件selfEditor.xml,两种配置方式,一种为propertyEditorRegistrars,另外一种customEditors;
5,编写测试类TestSelfEditor

1,编写实体类

Address.java

package com.mashibing.selfEditor;

public class Address {
private String provice;
private String city;
private String town;

public String getProvice() {
return provice;
}

public void setProvice(String provice) {
this.provice = provice;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getTown() {
return town;
}

public void setTown(String town) {
this.town = town;
}

@Override
public String toString() {
return "Address{" +
"provice='" + provice + '\'' +
", city='" + city + '\'' +
", town='" + town + '\'' +
'}';
}
}
Customer.java
2、定义属性编辑器
AddressPropertyEditor.java
package com.mashibing.selfEditor;

import java.beans.PropertyEditorSupport;

public class AddressPropertyEditor extends PropertyEditorSupport {

@Override
public void setAsText(String text) throws IllegalArgumentException {
String[] s=text.split("_");
Address address=new Address();
address.setProvice(s[0]);
address.setCity(s[1]);
address.setCity(s[2]);
this.setValue(address);
}
}
3、定义属性编辑注册器
AddressPropertyEditorRegistrar.java
package com.mashibing.selfEditor;

import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;

public class AddressPropertyEditorRegistrar implements PropertyEditorRegistrar {
@Override
public void registerCustomEditors(PropertyEditorRegistry propertyEditorRegistry) {
propertyEditorRegistry.registerCustomEditor(Address.class,new AddressPropertyEditor());
}
}
4、配置文件
selfEditor.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.xsd">

<bean id="customer" class="com.mashibing.selfEditor.Customer">
<property name="name" value="qiangShan"></property>
<property name="address" value="江西省_抚州市_南城县"></property>
</bean>

<!-- <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="propertyEditorRegistrars">
<list>
<bean id="addressPropertyEditorRegistrar" class="com.mashibing.selfEditor.AddressPropertyEditorRegistrar"></bean>
</list>
</property>
</bean>-->
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="com.mashibing.selfEditor.Address">
<value>com.mashibing.selfEditor.AddressPropertyEditor</value>
</entry>
</map>
</property>
</bean>
</beans>
5、测试类
TestSelfEditor.java
import com.mashibing.selfEditor.Customer;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSelfEditor {

@Test
public void test(){
ApplicationContext context=new ClassPathXmlApplicationContext("selfEditor.xml");
Customer customer = context.getBean("customer", Customer.class);
System.out.println(customer);
}
}
posted @ 2023-09-23 21:35  至尊只有一个  阅读(26)  评论(0编辑  收藏  举报