今天看了一下spring的Data Binding,觉得挺强大,记录一下,以免忘记。
public class User{
private String name;
private Son son;
public Son getSon() {
return son;
}
public void setSon(Son son) {
this.son = son;
}
}
public class Son{
private String name;
public Son(){}
public Son(String name) {
this.name = name;
}
}
<bean id="user" class="com.lhacker.domain.User">
<property name="name" value="Dylan" />
<property name="son" value="Dylan的儿子"/>
</bean>
不需要配置son的bean,只需要以上的一个配置,spring就会初始化Son,并把“Dylan的儿子"根据Son的有参构造函数,把值绑定在name属性中。
还可以定义自己的TypeEditor,继承java提供的PropertyEditorSuppor.
public class ExoticTypeEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(new Son(text.toUpperCase()));
}
}
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors"> <map> <entry key="com.lhacker.domain.Son"
value="com.lhacker.controller.util.ExoticTypeEditor" /> </map> </property>
</bean>
这样,就可以将Son的name属性值绑定为"DYLAN的儿子"。
还有另一种方式
public class CustomPropertyEditorRegistrar implements PropertyEditorRegistrar {
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(Son.class, new ExoticTypeEditor());
}
}
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="propertyEditorRegistrars">
<list>
<ref bean="customPropertyEditorRegistrar" />
</list>
</property>
</bean>
<bean id="customPropertyEditorRegistrar" class="com.lhacker.controller.util.CustomPropertyEditorRegistrar" />
分享到:
相关推荐
NULL 博文链接:https://bijian1013.iteye.com/blog/2164121
2. Spring 2.0 的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 更简单的XML配置 2.2.2. 新的bean作用域 2.2.3. 可扩展的XML编写 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的...
Spring.NET是一个应用程序框架,其目的是协助开发人员创建企业级的.NET应用程序。...32.3.PropertyEditor和TypeConverter 32.4.ResourceBundle和ResourceManager 32.5.异常 32.6.应用程序配置 32.7.AOP框架
实作 Validator 使用 PropertyEditor 档案上传 <br> <br>View层方案、Web框架整合 当使用JSP作为View层技术时,您可以结合JSTL以及Spring提供的标签,而除了JSP技术作为View层之外,Spring还...
5.4.2. 内建的PropertyEditor实现 6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 6.1.3. Spring的AOP代理 6.2. @AspectJ支持 6.2.1. 启用@AspectJ支持 ...
5.4.2. 内建的PropertyEditor实现 6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 6.1.3. AOP代理 6.2. @AspectJ支持 6.2.1. 启用@AspectJ支持 6.2.2. 声明...
5.4.2. 内建的PropertyEditor实现 6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 6.1.3. Spring的AOP代理 6.2. @AspectJ支持 6.2.1. 启用@AspectJ支持 ...
5.4.2. 内建的PropertyEditor实现 6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 6.1.3. AOP代理 6.2. @AspectJ支持 6.2.1. 启用@AspectJ支持 6.2.2. 声明...
强大的ConversionService,让很多梦想成真 基于ConversionService体系,定义自定义的类型转换器 格式化:带格式字符串内部对象 相互转换 使用支持格式化的转换器 数据校验框架 JSR 303 数据校验框架 如何使用...
强大的ConversionService,让很多梦想成真 基于ConversionService体系,定义自定义的类型转换器 格式化:带格式字符串内部对象 相互转换 使用支持格式化的转换器 数据校验框架 JSR 303 数据校验框架 如何...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
NULL 博文链接:https://b-l-east.iteye.com/blog/1705872
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
在本篇文章里小编给大家分享的是一篇关于Spring Boot定制PropertyEditors的知识点内容,有需要的朋友们可以参考学习下。