Spring中的BeanDefinition系列(二)—— AbstractBeanDefinition

前言

AbstractBeanDefinition是最终全功能BeanDefinition实现类的基类 。因此它非常重要,搞明白了它,基本上也就明白了一大部分的Spring中的Bean组件。

​ 一个因为实现了BeanDefinition接口的类有一些共用的方法和属性,因此需要一个类来完成它们公用的方法和属性,而AbstractBeanDefinition则扮演了这样一个角色。

Spring中的AbstractBeanDefinition

Spring中的AbstractBeanDefinition的属性(除去BeanDefinition中的)

属性名作用
INFER_METHOD(常量)若Bean未指定销毁方法,容器应该尝试推断Bean的销毁方法的名字,目前来说,推断的销毁方法的名字一般为close或是shutdown (即未指定Bean的销毁方法,但是内部定义了名为close或是shutdown的方法,则容器推断其为销毁方法)
autowireMode自动装配模式(AUTOWIRE_NO,AUTOWIRE_BY_NAME,AUTOWIRE_BY_TYPE,AUTOWIRE_CONSTRUCTOR,AUTOWIRE_AUTODETECT)
dependencyCheck依赖检查模式(DEPENDENCY_CHECK_NONE,DEPENDENCY_CHECK_OBJECTS,DEPENDENCY_CHECK_SIMPLE,DEPENDENCY_CHECK_ALL)
methodOverridesSet of method overrides, determining which, if any, methods on a managed object the Spring IoC container will override at runtime.
nonPublicAccessAllowedSpecify whether to allow access to non-public constructors and methods
lenientConstructorResolution确定构造函数是是否使用宽松构造的方式。(这个属性默认值是true,在大部分情况下都是使用宽松模式,即使多个构造函数的参数数量相同、类型存在父子类、接口实现类关系也能正常创建bean。)
syntheticwhether this bean definition is ‘synthetic’, that is, not defined by the application itself (for example, an infrastructure bean such as a helper for auto-proxying, created through {@code aop:config}).
initMethodNameSet the name of the initializer method. The default is {@code null}
qualifiersuse for autowire candidate resolution
Spring中@Primary和@Qualifier注解的区别

通过观察上面的各个属性,有一个是红色的,这是我们认为比较重要的一个属性: methodOverrides 。接下来对其进行分析。

methodOverrides

前言

​ 就像我前面说的,Bean之于Spring,就像员工之于公司。那么公司能不管理员工的行为吗?答案肯定是不能,同理,Spring能不管理Bean的方法吗,答案也肯定是不能。那么如何管理Bean的行为呢 ?

Spring使用了一个超级简单的方法,即使用MethodOverrides,用它来管理Bean中的方法。

作用

用于对bean已经存在的方法进行注入。

使用

那么是什么条件才会触发这个MethodOverrides呢?

其实是Spring配置文件中的lookup-method和replace-method, 这其实是两个方法级别的注入,和一般的属性(Property)注入是不一样的, 它们注入的是方法(Method)。

两者的差别是这样的 :

  • replace-method既可以用来替换有返回值的方法,也可以用来替换无返回值的函数,且 必须实现MethodReplacer接口的Bean才能替换 。

eg: spring容器标签解析之replaced-method

  • lookup-method 用于替换有返回值的方法。

eg: spring容器标签解析之lookup-method

深入

如上所述,methodOverrides用于对Bean中的方法进行管理,这让我们想到了Spring的AOP,那么AOP是不是就是使用methodOverrides来实现的呢?答案是并不是这样的,MethodOverride类中有如下的标注:

Note that the override mechanism is not intended as a generic means of inserting crosscutting code: use AOP for that.

由此可见,methodOverrides并不是实现AOP的通用方法。尽管使用它也可以来强行来实现AOP的功能,但是大概是过于复杂,所以官方通用的AOP并不是用它来进行实现的。

Spring中的AbstractBeanDefinition的方法(除去BeanDefinition中的)

方法作用
AbstractBeanDefinition()Create a new AbstractBeanDefinition with default settings.
AbstractBeanDefinition(ConstructorArgumentValues cargs, MutablePropertyValues pvs)Create a new AbstractBeanDefinition with the given constructor argument values and property values.
AbstractBeanDefinition(BeanDefinition original)Create a new AbstractBeanDefinition as a deep copy of the given bean definition.
overrideFrom(BeanDefinition other)Override settings in this bean definition (presumably a copied parent from a parent-child inheritance relationship) from the given bean definition (presumably the child).
applyDefaults(BeanDefinitionDefaults defaults)Apply the provided default values to this bean.
prepareMethodOverrides()Validate and prepare the method overrides defined for this bean.Checks for existence of a method with the specified name.
prepareMethodOverride(MethodOverride mo)Validate and prepare the given method override.Checks for existence of a method with the specified name,marking it as not overloaded if none found.
Spring中AbstractBeanDefinition的类图

在这里插入图片描述

参考链接

Spring的bean定义 2 : 通用bean定义逻辑 – AbstractBeanDefinition

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值