Mina 配置中的 CustomEditorConfigurer
太阳火神的美丽人生 (http://blog.csdn.net/opengl_es)
本文遵循“署名-非商业用途-保持一致”创作公用协议
<!--此部分被 NioSocketAcceptor 隐式使用,无此则会报字符串无法转换成 InetSocketAddress --> <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="java.net.SocketAddress" value="org.apache.mina.integration.beans.InetSocketAddressEditor" /> </map> </property> </bean>
可以想到,从字符串到 SocketAddress 的转换,会偿试使用该自定义属性编辑器。
----- 补充开始 ------
Spring 源码下载:https://github.com/spring-projects/spring-framework/tree/3.2.x
分析没有完成,大体了解到,Spring 配置文件中的一个 bean 并不全都会有目标装配位置,而是隐式地被 Spring 框架加载用于解析特定的内容。
比如这里:
<bean id="ioAcceptor" class="org.apache.mina.transport.socket.nio.NioSocketAcceptor" init-method="bind" destroy-method="unbind"> <property name="defaultLocalAddress" value=":12345" /> <property name="handler" ref="serverHandler" /> <property name="filterChainBuilder" ref="filterChainBuilder" /> <property name="reuseAddress" value="true" /> </bean>其中,defaultLocalAddress 的值是字符串形式提供,但实际 Mina 框架中的使用是这样的:
public void setDefaultLocalAddress(InetSocketAddress localAddress) { setDefaultLocalAddress(localAddress); }故而,Spring 会自动从配置文件中查找并实例化自定义的属性编辑器注册给自已用,当需要将字符串赋给该类型的属性时,就会使用已注册的对应类型的属性编辑器来处理。
上面已经提到,如果你把对应的配置文件中的地址转换属性编辑器注释掉,Spring 在进行转换处理时,查不到该类型的属性编辑器,就会提示错误,无法转换,当然了,是依据
这里面的 key 所指定的类型来查找的。
当然了,属性的类型,应该是通过反射得知的。
----- 补充结束 ------
具体属性编辑器的用法,还需进一步研究,暂时只知道由它转换即可。
经查 Spring 官方文档,获得如下 CustomEditorConfigurer 的类说明:
Class CustomEditorConfigurer
- java.lang.Object
-
- org.springframework.beans.factory.config.CustomEditorConfigurer
-
- All Implemented Interfaces:
- BeanFactoryPostProcessor, Ordered
public class CustomEditorConfigurer extends Object implements BeanFactoryPostProcessor, Ordered
implementation that allows for convenient registration of customBeanFactoryPostProcessor
接口的实现,它允许方便地注册自定义的属性编辑器 。
BeanFactoryPostProcessorproperty editors
.In case you want to register
PropertyEditor
instances, the recommended usage as of Spring 2.0 is to use customPropertyEditorRegistrar
implementations that in turn register any desired editor instances on a givenregistry
. 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 registerPropertyEditor
classes via thecustomEditors
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="mypackage.MyCustomDateEditor"/> <entry key="mypackage.MyObject" value="mypackage.MyObjectEditor"/> </map> </property> </bean>
注意,以下情况不能使用属性来注册属性编辑器:
Note, that you shouldn't registerPropertyEditor
bean instances via thecustomEditors
property asPropertyEditor
s are stateful and the instances will then have to be synchronized for every editing attempt. In case you need control over the instantiation process ofPropertyEditor
s, 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)