首页 > 代码库 > Mina 配置中的 CustomEditorConfigurer

Mina 配置中的 CustomEditorConfigurer

Mina 配置中的 CustomEditorConfigurer

太阳火神的美丽人生 (http://blog.csdn.net/opengl_es)

本文遵循“署名-非商业用途-保持一致”创作公用协议

转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino否则,出自本博客的文章拒绝转载或再转载,谢谢合作。


近期分析一个 Mina 的实际应用系统,发现在 Spring 依赖注入的配置文件中 CustomEditorConfigurer 的 bean 并未被任何地址使用进行装配,但把它去掉,就会报错,提示无法将字符串的地址转换成 InetSocketAddress 地址:

	<!--此部分被 NioSocketAcceptor 隐式使用,无此则会报字符串无法转换成 InetSocketAddress -->
	<bean id="customEditorConfigurer"
		class="org.springframework.beans.factory.config.CustomEditorConfigurer">
		<property name="customEditors">
			<map>
				<entry key="java.net.SocketAddress"
					value=http://www.mamicode.com/"org.apache.mina.integration.beans.InetSocketAddressEditor" />>

可以想到,从字符串到 SocketAddress 的转换,会偿试使用该自定义属性编辑器。


具体属性编辑器的用法,还需进一步研究,暂时只知道由它转换即可。



经查 Spring 官方文档,获得如下 CustomEditorConfigurer 的类说明:

org.springframework.beans.factory.config

Class CustomEditorConfigurer

  • java.lang.Object
    • org.springframework.beans.factory.config.CustomEditorConfigurer
  • All Implemented Interfaces:
    BeanFactoryPostProcessor, Ordered


    public class CustomEditorConfigurer
    extends Object
    implements BeanFactoryPostProcessor, Ordered
    BeanFactoryPostProcessor 接口的实现,它允许方便地注册自定义的 属性编辑器 。
    BeanFactoryPostProcessor
     implementation that allows for convenient registration of custom property editors.

    In case you want to register PropertyEditor instances, the recommended usage as of Spring 2.0 is to use custom PropertyEditorRegistrar implementations that in turn register any desired editor instances on a given registry. Each PropertyEditorRegistrar can register any number of custom editors.

     <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
       <property name="propertyEditorRegistrars">
         <list>
           <bean class="mypackage.MyCustomDateEditorRegistrar"/>
           <bean class="mypackage.MyObjectEditorRegistrar"/>
         </list>
       </property>
     </bean>
     

    最好通过属性  customEditors 注册  PropertyEditor 类。Spring 会为每一次编辑意向创建全新的该类的实例:
    It‘s perfectly fine to register PropertyEditor classes via the customEditors property. Spring will create fresh instances of them for each editing attempt then:

     <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
       <property name="customEditors">
         <map>
           <entry key="java.util.Date" value=http://www.mamicode.com/"mypackage.MyCustomDateEditor"/>>

    注意,以下情况不能使用属性来注册属性编辑器:
    Note, that you shouldn‘t register PropertyEditor bean instances via the customEditors property as PropertyEditors are stateful and the instances will then have to be synchronized for every editing attempt. In case you need control over the instantiation process of PropertyEditors, use aPropertyEditorRegistrar to register them.

    Also supports "java.lang.String[]"-style array class names and primitive class names (e.g. "boolean"). Delegates to ClassUtils for actual class name resolution.

    NOTE: Custom property editors registered with this configurer do not apply to data binding. Custom editors for data binding need to be registered on the DataBinder: Use a common base class or delegate to common PropertyEditorRegistrar implementations to reuse editor registration there.

    Since:
    27.02.2004
    Author:
    Juergen Hoeller
    See Also:
    PropertyEditor, PropertyEditorRegistrar, ConfigurableBeanFactory.addPropertyEditorRegistrar(org.springframework.beans.PropertyEditorRegistrar), ConfigurableBeanFactory.registerCustomEditor(java.lang.Class<?>, java.lang.Class<? extends java.beans.PropertyEditor>),DataBinder.registerCustomEditor(java.lang.Class<?>, java.beans.PropertyEditor)











Mina 配置中的 CustomEditorConfigurer