前言
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) |
methodOverrides | Set of method overrides, determining which, if any, methods on a managed object the Spring IoC container will override at runtime. |
nonPublicAccessAllowed | Specify whether to allow access to non-public constructors and methods |
lenientConstructorResolution | 确定构造函数是是否使用宽松构造的方式。(这个属性默认值是true,在大部分情况下都是使用宽松模式,即使多个构造函数的参数数量相同、类型存在父子类、接口实现类关系也能正常创建bean。) |
synthetic | whether 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}). |
initMethodName | Set the name of the initializer method. The default is {@code null} |
qualifiers | use 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. |