0%

spring自定义属性编辑器

spring自定义属性编辑器

属性编辑器用来解析bean的配置文件中的属性标签,spring的BeanWrapperImpl默认会注册CustomCollectionEditor(集合)、CustomMapEditor(Map)、CurrencyEditor(货币)、ByteArrayPropertyEditor等,可以使用CustomEditorConfigurer来注册属性编辑器,CustomEditorConfigurer实现了BeanFactoryPostProcessor接口,由spring容器自动检测并执行

步骤

  • 创建一个实现PropertyEditorRegistrar接口的类,重写registerCustomEditors方法,负责向spring容器注册属性编辑器
  • PropertyEditorRegistrar实现类作为bean进行注册
  • 配置CustomEditorConfigurer这个bean,并写入属性propertyEditorRegistrars

欢迎关注我的其它发布渠道