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);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)