spring ListableBeanFactory接口中getBeansOfType和BeanFactory接口的isTypeMatch方法实现分析
来源:互联网 发布:java elasticsearch 编辑:程序博客网 时间:2024/06/10 22:41
public interface ListableBeanFactory extends BeanFactory {
........
<T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException;
........
}
public interface ListableBeanFactory extends BeanFactory {
........
boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException;
........
}
ListableBeanFactory接口,继承自BeanFactory接口。DefaultListableBeanFactory类可以认为是ListableBeanFactory接口的一个常用实现。根据类型获取bean,即getBeansOfType方法,依赖于isTypeMatch。
以DefaultListableBeanFactory为例,getBeansOfType方法内部的核心实现是调用doGetBeanNamesForType方法,private String[] doGetBeanNamesForType(Class<?> type, boolean includeNonSingletons, boolean allowEagerInit) 。在spring中,真正干事的都是do×××方法。
doGetBeanNamesForType方法内,会先获取目前已有的所有的bean名字String[] beanDefinitionNames = getBeanDefinitionNames();,然后循环比较每个bean是否符合类型要求,如果符合,添加到结果集合内,List<String> result = new ArrayList<String>();,最后将result结合返回。
判断bean是否符合类型要求时,需要用到isTypeMatch接口实现,在AbstractBeanFactory类内。AbstractBeanFactory.isTypeMatch方法内,会根据beanName获取对应的RootBeanDefinition对象属性RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);,然后通过调用AbstractAutowireCapableBeanFactory.predictBeanType方法找到beanName对应的class类,Class<?> beanType = predictBeanType(beanName, mbd, typesToMatch);。predictBeanType方法内,会根据beanName找到对应的class值,一般是从RootBeanDefinition属性中getBeanClass方法获取mbd.getBeanClass();,如果有hasInstantiationAwareBeanPostProcessors的情况,会稍微复杂一点,这里不做说明。总之,predictBeanType方法会返回beanName对应的class类。isTypeMatch方法内,最终是用这个返回类和参数中的class类进行对应的。
对比两个类型是否相等的代码只有一句,typeToMatch.isAssignableFrom(beanType);,这是个本地方法,无法追查代码。typeToMatch是isTypeMatch(String name, Class<?>targetType)中targetType的简单处理后的值,Class<?>typeToMatch = (targetType != null ? targetType : Object.class);,beanType是predictBeanType方法中根据beanName获取的对应的class类,Class<?> beanType = predictBeanType(beanName, mbd, typesToMatch);
Class类isAssignableFrom方法说明
public final class Class<T> implements java.io.Serializable,
GenericDeclaration,
Type,
AnnotatedElement {
...........
public native boolean isAssignableFrom(Class<?> cls);
...........
}
instanceof 针对实例
isAssignableFrom针对class对象
isAssignableFrom 是用来判断一个类Class1和另一个类Class2是否相同或是另一个类的超类或接口。
通常调用格式是
- Class1.isAssignableFrom(Class2)
调用者和参数都是 java.lang.Class 类型。
- public class Test {
- public static void main(String[] args) {
- List<String> list = new ArrayList<String>();
- System.out.println(list instanceof List);
- System.out.println(list instanceof ArrayList);
- System.out.println(list.getClass().isAssignableFrom(List.class));
- System.out.println(List.class.isAssignableFrom(list.getClass()));
- }
- }
结果:
true
true
false
true
其中instanceof是子-->父
isAssignableFrom是父-->子
list.getClass() = class java.util.ArrayList
List.class = interface java.util.List
List ×× = ArrayList 可以
ArrayList ×× = List 报错
boolean java.lang.Class.isAssignableFrom(Class<?> cls)
Determines if the class or interface represented by this Class
object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specifiedClass
parameter. It returnstrue
if so; otherwise it returnsfalse
. If this Class
object represents a primitive type, this method returnstrue
if the specifiedClass
parameter is exactly thisClass
object; otherwise it returnsfalse
.
Specifically, this method tests whether the type represented by the specified Class
parameter can be converted to the type represented by this Class
object via an identity conversion or via a widening reference conversion. SeeThe Java Language Specification, sections 5.1.1 and 5.1.4 , for details.
- Parameters:
- cls the
Class
object to be checked - Returns:
- the
boolean
value indicating whether objects of the typecls
can be assigned to objects of this class - Throws:
- NullPointerException - if the specified Class parameter is null.
- Since:
- JDK1.1
- spring ListableBeanFactory接口中getBeansOfType和BeanFactory接口的isTypeMatch方法实现分析
- Spring源码阅读-BeanFactory子接口ListableBeanFactory
- BeanFactory子接口ListableBeanFactory
- Spring的接口BeanFactory和ApplicationContext
- Spring学习05--BeanFactory接口和BeanDefinition接口
- Spring框架之IoC容器—ListableBeanFactory接口
- spring源码--BeanFactory、AutowireCapableBeanFactory、HierarchicalBeanFactory、ListableBeanFactory...
- spring源码解读:BeanFactory接口
- TaskServiceImpl代码分析(理解afterPropertiesSet()和getBeansOfType()方法)
- BeanFactory接口包含基本方法
- 实现 BeanFactoryAware 接口获取 BeanFactory
- 【第二章:源码解析】Spring的BeanFactory的接口注解
- ApplicationContext 接口容器和BeanFactory 接口容器
- 底层解惑-spring的IOC相关接口:BeanFactory与FactoryBean
- Spring中接口和实现类命名规范的问题
- Spring中BeanFactory产生Bean的方法
- Spring中BeanFactory产生Bean的方法
- Spring中BeanFactory产生Bean的方法
- 顺序线性表的基本操作(C语言实现)
- response.setContentType()的作用及参数
- js 有没有一个方法会影响到另一个方法
- 第四周 项目2
- TensorFlow 曲线拟合
- spring ListableBeanFactory接口中getBeansOfType和BeanFactory接口的isTypeMatch方法实现分析
- HDU2609 How many(字符串的最小表示法)
- HDU 1873 看病要排队
- 移动端的iscroll
- 基于spring+springmvc+mybatis的图书管理系统
- hosts设置和ssh设置
- 如何解决 Linux 虚拟机磁盘设备名不一致的问题
- MongoDB详解
- ubuntu16.04 编译freeswitch1.9的视频mod_av模块