Spring属性编辑器

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值