Spring 属性注入(一)JavaBean 内省机制在 BeanWrapper 中的应用

Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html)

Spring 中的属性注入也是基于 JDK 的 JavaBean 的内省,详见《JDK 之 JavaBean 内省机制》:https://www.cnblogs.com/binarylei/p/10204208.html

一、BeanWrapper 的使用

  1. @Test
  2. public void test() {
  3. // 1.1 beanWrapper
  4. BeanWrapper beanWrapper = new BeanWrapperImpl(new Company());
  5. //BeanWrapper beanWrapper = new BeanWrapperImpl(Company.class);
  6. // 2.1 属性注入
  7. beanWrapper.setPropertyValue("name", "company");
  8. // 2.2 也可以这样,自动转 int
  9. PropertyValue pv = new PropertyValue("total", "20");
  10. beanWrapper.setPropertyValue(pv);
  11. // 2.3 嵌套注入,autoGrowNestedPaths=true 时当属性为 null 时自动创建对象
  12. beanWrapper.setAutoGrowNestedPaths(true);
  13. beanWrapper.setPropertyValue("director.name", "director");
  14. beanWrapper.setPropertyValue("employees[0].name", "binarylei");
  15. // 3.1 获取实例
  16. Company company = (Company) beanWrapper.getWrappedInstance();
  17. // 3.2 获取属性
  18. int total = (int) beanWrapper.getPropertyValue("total");
  19. }
  20. // JavaBean 如下,省略 get/set 方法
  21. public class Company {
  22. private String name;
  23. private int total;
  24. private Employee director;
  25. private Employee[] employees;
  26. public static class Employee{
  27. private String name;
  28. private double salary;
  29. }
  30. }

那 Spring 是如何将一个字符串转化为 int 类型的呢?

二、BeanWrapper 属性注入

跟踪 setPropertyValue 代码到 AbstractNestablePropertyAccessor#processLocalProperty 方法

  1. // 简单属性注入,而 Array, Collection, Map 则走 processKeyedProperty 方法
  2. private void processLocalProperty(PropertyTokenHolder tokens, PropertyValue pv) {
  3. PropertyHandler ph = getLocalPropertyHandler(tokens.actualName);
  4. Object originalValue = pv.getValue();
  5. Object valueToApply = originalValue;
  6. // 1. 类型转换
  7. alueToApply = convertForProperty(
  8. tokens.canonicalName, oldValue, originalValue, ph.toTypeDescriptor());
  9. // 2. 利用 JavaBean 的内省机制设置属性值
  10. ph.setValue(valueToApply);
  11. }

processLocalProperty 主要完成了两件事:一是类型转换;二是设置属性值。convertForProperty 利用 JDK 的 PropertyEditorSupport 进行类型转换,Spring 中内置了一批转换器,当然也可以自定义。而 setValue 则是使用反射进行赋值,关键代码如下:(BeanWrapperImpl#BeanPropertyHandler#setValue)

  1. writeMethod.invoke(getWrappedInstance(), value)

我们再看一下 BeanPropertyHandler 是什么,其实 BeanPropertyHandler 只是对 PropertyDescriptor 的简单封装。代码如下:

  1. @Override
  2. protected BeanPropertyHandler getLocalPropertyHandler(String propertyName) {
  3. PropertyDescriptor pd = getCachedIntrospectionResults().getPropertyDescriptor(propertyName);
  4. return (pd != null ? new BeanPropertyHandler(pd) : null);
  5. }

三、PropertyEditor 在 BeanWrapper 中的应用

Spring 提供了两种类型的转换方式:一是 JDK 的 PropertyEditor;二是 Spring 提供的 ConversionService。

既然 JDK 已经提供了 PropertyEditor,Spirng 为什么还要自己造轮子 ConversionService?其实是 JDK 的 PropertyEditor 只能从 String 类型转换为其他类型,而 ConversionService 支持从任何类型的转化。这里只关注 PropertyEditor 方式。

BeanWrapper 将 JavaBean 类型转换都委托给了 TypeConverterDelegate 组件,这个组件有一个重要的属性 propertyEditorRegistry,可以通过这个注册器获取对应的属性编辑器 PropertyEditor。

  1. private final PropertyEditorRegistrySupport propertyEditorRegistry;

跟踪 AbstractNestablePropertyAccessor#convertForProperty 到 TypeConverterDelegate#convertIfNecessary 方法中。

  1. public <T> T convertIfNecessary(@Nullable String propertyName, @Nullable Object oldValue, @Nullable Object newValue,
  2. @Nullable Class<T> requiredType, @Nullable TypeDescriptor typeDescriptor) throws IllegalArgumentException {
  3. // 1. 用户自定义属性编辑器
  4. PropertyEditor editor = this.propertyEditorRegistry.findCustomEditor(requiredType, propertyName);
  5. // 2. Spring 默认属性编辑器
  6. if (editor == null) {
  7. editor = findDefaultEditor(requiredType);
  8. }
  9. // 3. 执行类型转换
  10. convertedValue = doConvertValue(oldValue, convertedValue, requiredType, editor);
  11. }

convertIfNecessary 方法将只是匹配可用的 PropertyEditor 而执行则交给 doConvertValue 完成,很显然 doConvertValue 会调用 PropertyEditor#setAsText 进行类型转换,每个方法只做一件事。

  1. // 判断是否要进行类型转换
  2. private Object doConvertValue(@Nullable Object oldValue, @Nullable Object newValue,
  3. @Nullable Class<?> requiredType, @Nullable PropertyEditor editor) {
  4. // 省略...
  5. Object convertedValue = newValue;
  6. if (convertedValue instanceof String) {
  7. if (editor != null) {
  8. // Use PropertyEditor's setAsText in case of a String value.
  9. String newTextValue = (String) convertedValue;
  10. return doConvertTextValue(oldValue, newTextValue, editor);
  11. } else if (String.class == requiredType) {
  12. returnValue = convertedValue;
  13. }
  14. }
  15. return returnValue;
  16. }
  17. // 调用 PropertyEditor 的 setAsText 进行类型转换
  18. private Object doConvertTextValue(@Nullable Object oldValue, String newTextValue, PropertyEditor editor) {
  19. try {
  20. editor.setValue(oldValue);
  21. } catch (Exception ex) {
  22. }
  23. editor.setAsText(newTextValue);
  24. return editor.getValue();
  25. }

每天用心记录一点点。内容也许不重要,但习惯很重要!

Spring 属性注入(一)JavaBean 内省机制在 BeanWrapper 中的应用的更多相关文章

  1. Spring 属性注入(二)BeanWrapper 结构

    Spring 属性注入(二)BeanWrapper 结构 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) BeanWrap ...

  2. Spring 属性注入(三)AbstractNestablePropertyAccessor

    Spring 属性注入(三)AbstractNestablePropertyAccessor Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117 ...

  3. Spring 属性注入(四)属性键值对 - PropertyValue

    Spring 属性注入(四)属性键值对 - PropertyValue Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) P ...

  4. Spring属性注入、构造方法注入、工厂注入以及注入参数(转)

    Spring 是一个开源框架. Spring 为简化企业级应用开发而生(对比EJB2.0来说). 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能.Spring ...

  5. spring 属性注入

    Spring的核心技术室依赖注入,下面是依赖注入之属性注入的实现过程,牛刀小试,请看效果. 1.首先添加Spring.Web引用.本例中是使用分层思想来演示的,下面是项目的结构和UserModel类的 ...

  6. 六 Spring属性注入的四种方式:set方法、构造方法、P名称空间、SPEL表达式

    Spring的属性注入: 构造方法的属性注入 set方法的属性注入

  7. spring属性注入

    1,set方法注入 (1)对于值类型的属性: 在对象中一定要有set方法 package com.songyan.demo1; import com.songyan.injection.Car; /* ...

  8. java spring属性注入

    一.创建对象时候,向类里面属性设置值:一般有三个方式 1) .有参构造, 2). set**** 3).接口注入 二. 在spring框架里面,支持前面两种方式: 1).有参构造方法  用constr ...

  9. spring属性注入DI

    spring setter方式注入: 注入对象属性: 前提: 在bean对应实体中有对应的setter方法. 基础代码: 在bean中有另一个bean属性的setter方法. package cn.i ...

随机推荐

  1. Python教程_简介2

    人生苦短,我用Python--Life is short,you need Python. https://www.bilibili.com/video/av14184325/?p=101 Pytho ...

  2. spring boot 代理(not eligible for auto-proxying)

    spring 事务机制网上的案例很多,关于事务 不能回滚也有很多的类型,不同的问题有不同的处理方案,本篇博客主要介绍两种事务不能回滚的问题解决方案: 问题一:    在同一个对象中有两个方法,分别未方 ...

  3. Java(8)中List的遍历方式

    ============Java8之前的方式==========Map<String, Integer> items = new HashMap<>();items.put(& ...

  4. because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled

    spring security 配置问题,静态资源未被允许访问

  5. 【Django】关于使用阿里的iconfont

    刚刚从看到课程里老师使用了阿里提供的矢量图标iconfont.cn 我记录一下基本步骤: 1.登录iconfont.cn,搜索图标 2.选中想要的icon点击添加入库,再从右上方点购物车,把icon添 ...

  6. SQL%ROWCOUNT作用

    SQL%ROWCOUNT是一个游标属性,而SQL中的DML操作实际上是一种隐式的游标操作,在做insert,update,delete,merge以及select into操作时,Oracle会打开一 ...

  7. kafka NoNode错误

    报错 bin/kafka-run-class.sh kafka.tools.ConsumerOffsetChecker --zookeeper localhost:2181 --group=app-t ...

  8. vue element upload上传、清除等

    如果项目中可以使用file-list,那我们可以点击file-list删除文件列表: 有时候项目中是不要这个文件列表的,所以在上传成功以后,文件列表一直存在,要重新上传就必须刷新页面,所以我们需要手动 ...

  9. Codeforces Beta Round #73 (Div. 2 Only)

    Codeforces Beta Round #73 (Div. 2 Only) http://codeforces.com/contest/88 A 模拟 #include<bits/stdc+ ...

  10. [Java学习]面向对象-类的继承;方法覆盖

    一.类的继承 实现方法: public Class SubClass extends SuperClass{ } 继承最基本作用: 代码重用. 继承最重要的作用: 方法可以重写. 关于类的继承: 子类 ...