Spring PropertyEditor分析 侵立删

博客介绍了Spring中PropertyEditor的作用,它用于在字符串和Java类型间转换。阐述了Spring内置的PropertyEditor,还介绍了自定义PropertyEditor的方法及不同注入方式的优缺点。最后分析了框架中自定义PropertyEditor的实现原理,包括注册和实例化过程。

转自:https://blog.youkuaiyun.com/pentiumchen/article/details/44026575

 

简介

通过xml解析器解析出bean定义之后,解析出来的是一个一个的字符串,但是bean的属性可以各种java类型,那么在对bean进行初始化时需要在这些字符串和java类型之间进行转换,比如我的bean有个Class属性,那么在注入这个Class属性时在只能给property设置一个字符串value="a.b.C" ,框架在最终填充这个属性的时候需要把"a.b.C"转换成Class对象,这项工作是通过什么来完成的呢?它就是几天这篇博客的主角,PropertyEditor。

PropertyEditor的处理过程如下:

 

  • 调用setAsText把待处理的text传递到PropertyEditor,PropertyEditor把text转换成相应类型的值并且保存到PropertyEditor
  • 调用getValue把获取上面步骤转换后的value

 

通过上面的过程可以发现PropertyEditor保存了value,所以是非线程安全的,因此在值转换的过程中需要做同步

内置PropertyEditor

Spring框架内置了一些PropertyEditor来对,并且bean填充属性之前都会把这些PropertyEditor添加到BeanWrapper中(在Spring IOC容器bean初始化源码分析有分析),代码在PropertyEditorRegistrySupport类的createDefaultEditors方法中:

 

 
  1. private void createDefaultEditors() {

  2. this.defaultEditors = new HashMap<Class<?>, PropertyEditor>(64);

  3.  
  4. // Simple editors, without parameterization capabilities.

  5. // The JDK does not contain a default editor for any of these target types.

  6. this.defaultEditors.put(Charset.class, new CharsetEditor());

  7. this.defaultEditors.put(Class.class, new ClassEditor());

  8. this.defaultEditors.put(Class[].class, new ClassArrayEditor());

  9. this.defaultEditors.put(Currency.class, new CurrencyEditor());

  10. this.defaultEditors.put(File.class, new FileEditor());

  11. this.defaultEditors.put(InputStream.class, new InputStreamEditor());

  12. this.defaultEditors.put(InputSource.class, new InputSourceEditor());

  13. this.defaultEditors.put(Locale.class, new LocaleEditor());

  14. this.defaultEditors.put(Pattern.class, new PatternEditor());

  15. this.defaultEditors.put(Properties.class, new PropertiesEditor());

  16. this.defaultEditors.put(Resource[].class, new ResourceArrayPropertyEditor());

  17. this.defaultEditors.put(TimeZone.class, new TimeZoneEditor());

  18. this.defaultEditors.put(URI.class, new URIEditor());

  19. this.defaultEditors.put(URL.class, new URLEditor());

  20. this.defaultEditors.put(UUID.class, new UUIDEditor());

  21.  
  22. // Default instances of collection editors.

  23. // Can be overridden by registering custom instances of those as custom editors.

  24. this.defaultEditors.put(Collection.class, new CustomCollectionEditor(Collection.class));

  25. this.defaultEditors.put(Set.class, new CustomCollectionEditor(Set.class));

  26. this.defaultEditors.put(SortedSet.class, new CustomCollectionEditor(SortedSet.class));

  27. this.defaultEditors.put(List.class, new CustomCollectionEditor(List.class));

  28. this.defaultEditors.put(SortedMap.class, new CustomMapEditor(SortedMap.class));

  29.  
  30. // Default editors for primitive arrays.

  31. this.defaultEditors.put(byte[].class, new ByteArrayPropertyEditor());

  32. this.defaultEditors.put(char[].class, new CharArrayPropertyEditor());

  33.  
  34. // The JDK does not contain a default editor for char!

  35. this.defaultEditors.put(char.class, new CharacterEditor(false));

  36. this.defaultEditors.put(Character.class, new CharacterEditor(true));

  37.  
  38. // Spring's CustomBooleanEditor accepts more flag values than the JDK's default editor.

  39. this.defaultEditors.put(boolean.class, new CustomBooleanEditor(false));

  40. this.defaultEditors.put(Boolean.class, new CustomBooleanEditor(true));

  41.  
  42. // The JDK does not contain default editors for number wrapper types!

  43. // Override JDK primitive number editors with our own CustomNumberEditor.

  44. this.defaultEditors.put(byte.class, new CustomNumberEditor(Byte.class, false));

  45. this.defaultEditors.put(Byte.class, new CustomNumberEditor(Byte.class, true));

  46. this.defaultEditors.put(short.class, new CustomNumberEditor(Short.class, false));

  47. this.defaultEditors.put(Short.class, new CustomNumberEditor(Short.class, true));

  48. this.defaultEditors.put(int.class, new CustomNumberEditor(Integer.class, false));

  49. this.defaultEditors.put(Integer.class, new CustomNumberEditor(Integer.class, true));

  50. this.defaultEditors.put(long.class, new CustomNumberEditor(Long.class, false));

  51. this.defaultEditors.put(Long.class, new CustomNumberEditor(Long.class, true));

  52. this.defaultEditors.put(float.class, new CustomNumberEditor(Float.class, false));

  53. this.defaultEditors.put(Float.class, new CustomNumberEditor(Float.class, true));

  54. this.defaultEditors.put(double.class, new CustomNumberEditor(Double.class, false));

  55. this.defaultEditors.put(Double.class, new CustomNumberEditor(Double.class, true));

  56. this.defaultEditors.put(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, true));

  57. this.defaultEditors.put(BigInteger.class, new CustomNumberEditor(BigInteger.class, true));

  58.  
  59. // Only register config value editors if explicitly requested.

  60. if (this.configValueEditorsActive) {

  61. StringArrayPropertyEditor sae = new StringArrayPropertyEditor();

  62. this.defaultEditors.put(String[].class, sae);

  63. this.defaultEditors.put(short[].class, sae);

  64. this.defaultEditors.put(int[].class, sae);

  65. this.defaultEditors.put(long[].class, sae);

  66. }

  67. }

此外还内置了一些Resource相关的PropertyEditor,代码在ResourceEditorRegistrar类的registerCustomEditors方法中:

 
  1. public void registerCustomEditors(PropertyEditorRegistry registry) {

  2. ResourceEditor baseEditor = new ResourceEditor(this.resourceLoader, this.propertyResolver);

  3. doRegisterEditor(registry, Resource.class, baseEditor);

  4. doRegisterEditor(registry, ContextResource.class, baseEditor);

  5. doRegisterEditor(registry, InputStream.class, new InputStreamEditor(baseEditor));

  6. doRegisterEditor(registry, InputSource.class, new InputSourceEditor(baseEditor));

  7. doRegisterEditor(registry, File.class, new FileEditor(baseEditor));

  8. doRegisterEditor(registry, URL.class, new URLEditor(baseEditor));

  9.  
  10. ClassLoader classLoader = this.resourceLoader.getClassLoader();

  11. doRegisterEditor(registry, URI.class, new URIEditor(classLoader));

  12. doRegisterEditor(registry, Class.class, new ClassEditor(classLoader));

  13. doRegisterEditor(registry, Class[].class, new ClassArrayEditor(classLoader));

  14.  
  15. if (this.resourceLoader instanceof ResourcePatternResolver) {

  16. doRegisterEditor(registry, Resource[].class,

  17. new ResourceArrayPropertyEditor((ResourcePatternResolver) this.resourceLoader, this.propertyResolver));

  18. }

  19. }

ResourceEditorRegistrar是框架内置的一个PropertyEditor注册器,它是一个BFPP,在ClasspathXmlApplicationContext启动时就会把它添加到容器中,具体的代码分析在Spring IOC容器启动简介中。

 

自定义PropertyEditor

当Spring内置的PropertyEditor无法满足我们的要求的时候,我们可以根据Spring提供的扩展机制来自定义PropertyEditor,下面通过一个例子来介绍如何实现自定义的PropertyEditor,我的这个PropertyEditor是一个时间相关的Editor,它可以一个满足特定时间格式的字符串转换成日期对象。

 

首先定义一个PropertyEditor,框架中提供了一个PropertyEditor基类PropertyEditorSupport,直接继承这个类可以省去一部分代码,代码如下:

 

 
  1. public class DateEditor extends PropertyEditorSupport {

  2. private DateFormat dateFormat;

  3.  
  4. public void setDateFormat(String dateFormat) {

  5. this.dateFormat = new SimpleDateFormat(dateFormat);

  6. }

  7.  
  8. @Override

  9. public void setAsText(String text) throws IllegalArgumentException {

  10. try {

  11. Object value = dateFormat.parse(text);

  12. setValue(value);

  13. } catch (ParseException e) {

  14. e.printStackTrace();

  15. }

  16. }

  17.  
  18. @Override

  19. public String getAsText() {

  20. if (getValue() instanceof Date) {

  21. Date d = (Date) getValue();

  22. return dateFormat.format(d);

  23. }

  24. return super.getAsText();

  25. }

  26.  
  27. }

dateFormat是日期格式,需要使用者注入

接下来要把DateEditor注入到到容器中,框架提供了一个BFPP 类CustomEditorConfigurer来完成这项工作,我们只要在DateEditor注入到这个BFPP bean中就可以了,有以下几种注入方式:

通过customEditors属性注入,它是CustomEditorConfigurer中 一个Map属性,key是类型字符串,value是PropertyEditor实例,xml配置片段如下:

 

 
  1. <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">

  2. <property name="customEditors">

  3. <map>

  4. <entry key="java.util.Date" value-ref="dateEditor"></entry>

  5. </map>

  6. </property>

  7. </bean>

  8. <bean id="dateEditor" class="spring.beans.propertyeditor.DateEditor">

  9. <property name="dateFormat" value="yyyy-MM-dd HH:mm:ss" />

  10. </bean>

通过一个测试bean和一段测试代码测试一下:

 

 
  1. <bean id="dateBean" class="spring.beans.propertyeditor.DateBean">

  2. <constructor-arg value="2014-06-01 09:21:20"></constructor-arg>

  3. </bean>

 
  1. @Test

  2. public void test() {

  3. BeanFactory context = new ClassPathXmlApplicationContext(

  4. "spring/beans/propertyeditor/propertyeditor1.xml");

  5. DateBean dateBean = (DateBean) context.getBean("dateBean");

  6. System.out.println(dateBean.getDate());

  7. }

打印结果

 

Sun Jun 01 09:21:20 CST 2014

说明日志转成功了

但是这种方式有一个问题,在这种配置方式下,PropertyEditor是共享的,在博客最开始的时候提到了PropertyEditor是非线程安全的,所以在处理的过程中需要做同步,这意味着在在高并发坏境下这里的同步会对应用的性能造成比较大的影响。所以这种方式是不推荐的,事实上这种方式已经被Spring打上deprecated标签了。

为了解决上面提到的可能引发的性能问题,我们可以修改配置:

 

 
  1. <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">

  2. <property name="customEditors">

  3. <map>

  4. <entry key="java.util.Date" value="spring.beans.propertyeditor.DateEditor"></entry>

  5. </map>

  6. </property>

  7. </bean>

上面这段配置是把DateEditor的class注入到customEditors属性中,这种情况下在bean初始化时会创建一个新的PropertyEditor对象注册到BeanWrapper中(可以参考Spring IOC容器bean初始化源码分析中的分析),这样PropertyEditor是不同的对象状态是独立的,就不会有上面提到的同步问题。但是还是有个问题,这个DateEditor是需要注入属性的,而PropertyEditor的实例化又不能被应用层干预,所以无法注入这个dateFormat属性到DateEditor对象中,因此这种方式是不能满足我们的需求的,它只能应用在PropertyEditor不需要注入属性的情况。对于我们的需求只能通过下面这种方式来实现。

通过propertyEditorRegistrars注入,它是CustomEditorConfigurer的一个PropertyEditor注册器数组,它的职责是用来注册PropertyEditor,而且可以注册任意数量的PropertyEditor,首先需要定义一个注册器:

 

 
  1. public class CustomPropertyEditorRegistrar implements PropertyEditorRegistrar {

  2.  
  3. private String dateFormat;

  4.  
  5. public String getDateFormat() {

  6. return dateFormat;

  7. }

  8.  
  9. public void setDateFormat(String dateFormat) {

  10. this.dateFormat = dateFormat;

  11. }

  12.  
  13. @Override

  14. public void registerCustomEditors(PropertyEditorRegistry registry) {

  15. DateEditor dateEditor = new DateEditor();

  16. dateEditor.setDateFormat(dateFormat);

  17. registry.registerCustomEditor(Date.class, dateEditor);

  18.  
  19. }

  20.  
  21. }

在注册器的registerCustomEditors方法中把DateEditor注册到容器中并且和Date类型绑定,要确保DateEditor对象时临时创建的对象而不是一个全局对象,否则一样可能会引发性能问题。把日期格式通过dateFormat字段注入,然后修改xml片段:

 

 
  1. <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">

  2. <property name="propertyEditorRegistrars">

  3. <list>

  4. <bean class="spring.beans.propertyeditor.CustomPropertyEditorRegistrar">

  5. <property name="dateFormat" value="yyyy-MM-dd HH:mm:ss"></property>

  6. </bean>

  7. </list>

  8. </property>

  9. </bean>

这种方式下,每个BeanWrapper都会注册不同的PropertyEditor,不会有高并发下的性能问题,而且用法也比较灵活,所以Spring的推荐是通过propertyEditorRegistrars注入来完成PropertyEditor的自定义。

 

代码原理

下面来看看关于框架中关于自定义PropertyEditor的实现

 

看下CustomEditorConfigurer类的postProcessBeanFactory方法:

 
  1. public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

  2. if (this.propertyEditorRegistrars != null) {

  3. for (PropertyEditorRegistrar propertyEditorRegistrar : this.propertyEditorRegistrars) {

  4. beanFactory.addPropertyEditorRegistrar(propertyEditorRegistrar);

  5. }

  6. }

  7.  
  8. if (this.customEditors != null) {

  9. for (Map.Entry<String, ?> entry : this.customEditors.entrySet()) {

  10. String key = entry.getKey();

  11. Object value = entry.getValue();

  12. Class requiredType = null;

  13.  
  14. try {

  15. requiredType = ClassUtils.forName(key, this.beanClassLoader);

  16. if (value instanceof PropertyEditor) {

  17. ...

  18. beanFactory.addPropertyEditorRegistrar(

  19. new SharedPropertyEditorRegistrar(requiredType, (PropertyEditor) value));

  20. }

  21. else if (value instanceof Class) {

  22. beanFactory.registerCustomEditor(requiredType, (Class) value);

  23. }

  24. else if (value instanceof String) {

  25. Class editorClass = ClassUtils.forName((String) value, this.beanClassLoader);

  26. Assert.isAssignable(PropertyEditor.class, editorClass);

  27. beanFactory.registerCustomEditor(requiredType, editorClass);

  28. }

  29. else {

  30. ...

  31. }

  32. }

  33. catch (ClassNotFoundException ex) {

  34. ...

  35. }

  36. }

  37. }

  38. }


 

从代码中可以看到:

把propertyEditorRegistrars中所有的PropertyEditor注册器通过调用beanFactory的addPropertyEditorRegistrar方法注册到容器中,保存在propertyEditorRegistrars集合属性中

如果customEditors属性value的类型为String或Class,调用beanFactory的registerCustomEditor把自定义PropertyEditor的Class添加到容器中(String转换成Class),保存在容器的customEditors哈希表属性中,key为值类型,value为PropertyEditor类型。

如果customEditors属性value是一个PropertyEditor实例,那么注册一个PropertyEditor注册器SharedPropertyEditorRegistrar到容器中(同样是调用beanFactory的addPropertyEditorRegistrar方法),该注册器保存这个PropertyEditor实例,并且BeanWrapper进行初始化工作时会把这个PropertyEditor实例注册到BeanWrapper中,无论哪个BeanWrapper注册的都是这同一个实例。

通过Spring IOC容器bean初始化源码分析中的分析,bean实例化时会调用AbstractBeanFactory的initBeanWrapper方法,这个方法紧接着都调用registerCustomEditors方法,看看registerCustomEditors方法中的代码:

 

 
  1. protected void registerCustomEditors(PropertyEditorRegistry registry) {

  2. PropertyEditorRegistrySupport registrySupport =

  3. (registry instanceof PropertyEditorRegistrySupport ? (PropertyEditorRegistrySupport) registry : null);

  4. if (registrySupport != null) {

  5. registrySupport.useConfigValueEditors();

  6. }

  7. if (!this.propertyEditorRegistrars.isEmpty()) {

  8. for (PropertyEditorRegistrar registrar : this.propertyEditorRegistrars) {

  9. try {

  10. registrar.registerCustomEditors(registry);

  11. }

  12. catch (BeanCreationException ex) {

  13. ...

  14. }

  15. }

  16. }

  17. if (!this.customEditors.isEmpty()) {

  18. for (Map.Entry<Class<?>, Class<? extends PropertyEditor>> entry : this.customEditors.entrySet()) {

  19. Class<?> requiredType = entry.getKey();

  20. Class<? extends PropertyEditor> editorClass = entry.getValue();

  21. registry.registerCustomEditor(requiredType, BeanUtils.instantiateClass(editorClass));

  22. }

  23. }

  24. }

registry这个参数就是BeanWrapper实例(BeanWrapperImpl实现了PropertyEditorRegistry接口,并且继承了PropertyEditorRegistrySupport类)从代码中可以看到:

 

  • 首先调用所有框架内置和自定义PropertyEditor注册器的registerCustomEditors方法,这个方法把执行PropertyEditor注册到BeanWrapper逻辑,比如在我们的CustomPropertyEditorRegistrar注册器就是就是通过registry.registerCustomEditor 方法把DateEditor实例添加到BeanWrapper中,注意这个实例是新鲜出炉的哟。而在上面提到的SharedPropertyEditorRegistrar注册器中,它里面持有一个之前注册进来的PropertyEditor对象,这个对象实例始终是同一个,是共享的,这个注册器在注册PropertyEditor时会先调用PropertyEditorRegistrySupport.registerSharedEditor方法,但是最终还是会调到PropertyEditorRegistrySupport.registerCustomEditor的方法,registerCustomEditor这个方法的作用就是把PropertyEditor保存在customEditors这个Map属性中,key是类型,value是PropertyEditor实例,BeanWrapper在进行值转换时直接从这个Map中获取相关的PropertyEditor。
  • 接下来处理customEditors中的PropertyEditor,这里面的value都收Class,所以先根据Class创建一个实例,然后调用registry.registerCustomEditor 把创建好的实例注册到BeanWrapper中,实例也是新鲜出炉的。

 

以上是关于Spring中PropertyEditor的全部内容。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值