概述
1 属性编辑器
对于属性编辑器我们一般分为Core Context的使用和MVC的使用两种
Core Context的使用
Spring Core Context其实也使用ConversionService,但是是非强制的。
Spring在读取xml配置文件的时候,因为xml文件实际上是一个文本文件,所有值的设置都是String,这个时候如果给bean的复杂类型属性设置值,它会用到PropertyEditor或ConversionService。
比如下面例子中的color属性是Color类型,这时就会利用到PropertyEditor和ConversionService:
<bean id="someBean" class="a.b.c.SomeBean">
<property name="color" value="red"/>
</bean>
MVC的使用
Spring MVC对于conversionService的使用比较特殊,它自己会注册一个名字叫做mvcConversionService类型为DefaultFormattingConversionService的Bean(代码在WebMvcConfigurationSupport#mvcConversionService)。因此会存在以下陷阱:
如果Core Context中也定义了一个ConversionService,那么在MVC环境下,会有两个ConversionService的Bean。
针对Core Context的ConversionService做的Customize如FormatterRegistrar、ConverterRegistry、FormatterRegistry、ConversionServiceFactoryBean、FormattingConversionServiceFactoryBean是不会应用到MVC的那个ConversionService上。 对于mvcConversionService的配置途径见这里。
2 用法
2.1 Core Context的使用
主要用运用:
PropertyEditorSupport类
PropertyEditorRegistrar
CustomEditorConfigurer
2.1.1 自定义注册编辑器
public class PointPropertEditor extends PropertyEditorSupport{
@Override
public void setAsText(String text) throws IllegalArgumentException {
String[] nums = text.split(",");
Point point = new Point();
point.setX(Integer.parseInt(nums[0]));
point.setY(Integer.parseInt(nums[1]));
setValue(point);
}
}
2.1.2 全局配置
@Component
public class CustomPropertyEditorRegistrars implements PropertyEditorRegistrar{
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(Point.class, new PointPropertEditor());
}
}
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="propertyEditorRegistrars">
<bean class="com.gz.spring.propertyEditor.CustomPropertyEditorRegistrars"/>
</property>
<bean>
2.1.1 局部配置
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="com.gz.test.entity.Point">
<bean class="com.gz.spring.propertyEditor.PointPropertEditorr">
</bean>
</entry>
</map>
</property>
</bean>
2.2 MVC的使用
主要用运用:
PropertyEditorSupport类
@InitBinder
WebBindingInitializer接口
2.2.1 自定义注册编辑器
public class DatePropertyEditor extends PropertyEditorSupport {
private static final Logger logger = LoggerFactory.getLogger(DatePropertyEditor.class);
@Override
public void setAsText(String text) throws IllegalArgumentException {
logger.info("类型转换 传入数据:"+text);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
setValue(df.parse(text));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
2.2.2 注册属性编辑器(注解)
2.2.2.1 单个类中@InitBinder注解(针对单个contoller)
@InitBinder
private void PointDataBinder(WebDataBinder webDataBinder) {
logger.info("类型转换开始");
webDataBinder.registerCustomEditor(Date.class, new DatePropertyEditor());
}
2.2.2.2 整个项目@InitBinder注解(全局)
@ControllerAdvice
public class InitBinderControllerAdvice {
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Dept.class, new CustomDeptEditor());
}
}
2.2.3 注册属性编辑器(编程)
2.2.3.1 实现 WebBindingInitializer接口(全局定义)
java代码:
public class DateWebBindingInitializer implements WebBindingInitializer{
@Override
public void initBinder(WebDataBinder binder, WebRequest request) {
binder.registerCustomEditor(Date.class, new DatePropertyEditor());
}
}
spring配置文件:
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer">
<bean class="com.mvc.editor.MyWebBindingInitializer" />
</property>
</bean>
原理
DispatcherServlet.doDispatch()->AbstractHandlerMethodAdapter.handler()->RequestMappingHandlerAdapter.invokeHandlerMethod()
->ServletInvocableHandlerMethod.invokeForRequest()->InvocableHandlerMethod.getMethodArgumentValues()->
在RequestMappingHandlerAdapter类中,在invokeHandlerMethod方法,会向InvocableHandlerMethod注入sDataBinderFactory
最后
以上就是魔幻高山为你收集整理的Spring属性编辑器1 属性编辑器2 用法的全部内容,希望文章能够帮你解决Spring属性编辑器1 属性编辑器2 用法所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复