Spring 自定义propertyEditor的示例代码
更新时间:2022年12月13日 10:51:39 作者:Acaak
这篇文章主要介绍了Spring 自定义propertyEditor的示例代码,本文结合示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下


User
package com.example.zylspringboot.selfEditor;
public class User {
private String name;
private Address address;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", address=" + address +
", age=" + age +
'}';
}
}
Address
package com.example.zylspringboot.selfEditor;
public class Address {
private String province;
private String city;
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Address{" + "province='" + province + '\'' + ", city='" + city + '\'' + '}';
}
}
SelfPropertyEditor
package com.example.zylspringboot.selfEditor;
import java.beans.PropertyEditorSupport;
public class SelfPropertyEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException {
String[] s = text.split("_");
Address address = new Address();
address.setCity(s[0]);
address.setProvince(s[1]);
super.setValue(address);
}
}
AcaakPropertyRegistor
package com.example.zylspringboot.selfEditor;
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
public class AcaakPropertyRegistor implements PropertyEditorRegistrar {
@Override
public void registerCustomEditors(PropertyEditorRegistry propertyEditorRegistry) {
propertyEditorRegistry.registerCustomEditor(Address.class,new SelfPropertyEditor());
}
}
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"
xmlns:context="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="user" class="com.example.zylspringboot.selfEditor.User">
<property name="age" value="18"></property>
<property name="name" value="Acaak"></property>
<property name="address" value="广东省_广州市"></property>
</bean>
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="propertyEditorRegistrars">
<list>
<bean class="com.example.zylspringboot.selfEditor.AcaakPropertyRegistor"></bean>
</list>
</property>
</bean>
或
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="com.example.zylspringboot.selfEditor.Address">
<value>com.example.zylspringboot.selfEditor.SelfPropertyEditor</value>
</entry>
</map>
</property>
</bean>
</beans>
到此这篇关于Spring 自定义propertyEditor的文章就介绍到这了,更多相关Spring 自定义propertyEditor内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:
相关文章
一个applicationContext 加载错误导致的阻塞问题及解决方法
这篇文章主要介绍了一个applicationContext 加载错误导致的阻塞问题及解决方法,需要的朋友可以参考下2018-11-11
解决maven build 无反应,直接terminated的问题
下面小编就为大家带来一篇解决maven build 无反应,直接terminated的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-06-06
IDEA的部署设置改为war exploded运行项目出错问题
在使用IDEA配置warexploded部署时,可能会遇到路径问题或404错误,解决方法是进入Deployment设置,删除Application content中的/marry_war_exploded,使其为空,然后重新运行项目即可,这是一种有效的解决策略,希望能帮助到遇到同样问题的开发者2024-10-10
Mybatis查询Sql结果未映射到对应得实体类上的问题解决
使用mybatis查询表数据得时候,发现对应得实体类字段好多都是null,本文主要介绍了Mybatis查询Sql结果未映射到对应得实体类上的问题解决,具有一定的参考价值,感兴趣的可以了解一下2024-02-02
SpringBoot整合Web之CORS支持与配置类和 XML配置及注册拦截器
这篇文章主要介绍了SpringBoot整合Web开发中CORS支持与配置类和 XML配置及注册拦截器的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2022-08-08


最新评论