掘金 后端 ( ) • 2024-04-26 11:15

spring自定义属性编辑器

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

步骤

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

https://zhhll.icu/2021/框架/spring/基础/17.spring自定义属性编辑器/

本文由mdnice多平台发布