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;标签:city,String,自定义,import,编辑器,provice,org,public,属性 From: https://www.cnblogs.com/shanqiang1/p/17725111.html
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);
}
}