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是否相同或是另一个类的超类或接口。   
通常调用格式是  

Java代码  收藏代码
  1. Class1.isAssignableFrom(Class2)     

 

调用者和参数都是   java.lang.Class   类型。   


  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.         List<String> list = new ArrayList<String>();  
  4.         System.out.println(list instanceof List);  
  5.         System.out.println(list instanceof ArrayList);  
  6.           
  7.         System.out.println(list.getClass().isAssignableFrom(List.class));  
  8.         System.out.println(List.class.isAssignableFrom(list.getClass()));  
  9.     }  
  10. }  

结果:

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 type cls can be assigned to objects of this class
Throws:
NullPointerException - if the specified Class parameter is null.
Since:
JDK1.1


阅读全文
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 从公务员调入事业单位的怎么办 电车被城管扣了怎么办 超变战陀发射器绳坏了怎么办 家里人总打击我怎么办干啥都要骂 欧陆风云4破产后怎么办 车被别人喷了漆怎么办 龙分期绑银行卡维护钱还不上怎么办 去维和要是伤了怎么办 头盔镜片刮花了怎么办 小牛u1钥匙丢了怎么办 非牛顿流体干了怎么办 刺激战场0信誉分怎么办 假如非牛顿干了怎么办? 退出id后照片了怎么办 小麦收割机麦糠有籽粒怎么办 在家带娃没钱花怎么办 看3d电影近视的怎么办 摩托车电瓶没电了怎么办 踏板摩托车电瓶没电了怎么办 摩托车离合油没了怎么办 论文数据计算错误该怎么办 脚被擦破皮了痛怎么办 脚撞到了很疼怎么办 腿不小心磕青了怎么办 木制椅子腿断了怎么办 实木椅子腿断了怎么办 铁质椅子腿断了怎么办 大套摩托车查车怎么办 摩托车本扣12分怎么办 蓝牙耳机上面的软套掉了怎么办 工作不想干了怎么办啊 浴室门玻璃坏了怎么办 电梯顶层安全安全距离不合格怎么办 制冰机有些孔不出水怎么办 制冰机排水孔堵住了怎么办 点我达装备抽查怎么办 空军常服大檐帽有一点变形怎么办 一般纳税人没有进项发票怎么办 汽车购买发票丢了怎么办 购买房子的发票丢了怎么办 购买的二手房发票丢失怎么办