Spring源码解析 - ListableBeanFactory

Extension of the {@link BeanFactory} interface to be implemented by bean factories
that can enumerate all their bean instances, rather than attempting bean lookup
by name one by one as requested by clients. BeanFactory implementations that
preload all their bean definitions (such as XML-based factories) may implement
this interface.

扩展BeanFactory接口,提供所有bean 实例的枚举,不再需要客户端通过一个个bean name查找.BeanFactory实现类预加载bean定义(如通过实现xml的工厂)需要实现这个接口.

If this is a {@link HierarchicalBeanFactory}, the return values will <i>not</i>
take any BeanFactory hierarchy into account, but will relate only to the beans
defined in the current factory. Use the {@link BeanFactoryUtils} helper class
to consider beans in ancestor factories too.
如果一样实现了HierarchicalBeanFactory,返回值不会考虑父类BeanFactory,只考虑当前factory定义的类.当然也可以使用BeanFactoryUtils辅助类来查找祖先工厂中的类.

The methods in this interface will just respect bean definitions of this factory.
They will ignore any singleton beans that have been registered by other means like
{@link org.springframework.beans.factory.config.ConfigurableBeanFactory}‘s
{@code registerSingleton} method, with the exception of
{@code getBeanNamesOfType} and {@code getBeansOfType} which will check
such manually registered singletons too. Of course, BeanFactory‘s {@code getBean}
does allow transparent access to such special beans as well. However, in typical
scenarios, all beans will be defined by external bean definitions anyway, so most
applications don‘t need to worry about this differentation.

这个接口中的方法只会考虑本factory定义的bean.这些方法会忽略ConfigurableBeanFactory的registerSingleton注册的单例bean,getBeanNamesOfType和getBeansOfType是例外,一样会考虑手动注册的单例.当然BeanFactory的getBean一样可以透明访问这些特殊bean.当然在典型情况下,所有的bean都是由external bean定义,所以应用不需要顾虑这些差别.
<b>NOTE:</b> With the exception of {@code getBeanDefinitionCount}
and {@code containsBeanDefinition}, the methods in this interface
are not designed for frequent invocation. Implementations may be slow.

注意:getBeanDefinitionCount和containsBeanDefinition的实现方法因为效率比较低,并不是供频繁调用的.

  1 package org.springframework.beans.factory;
  2 public interface ListableBeanFactory extends BeanFactory {
  3
  4     /**
  5      * 检查bean factory是否含有给定name的bean定义.
  6      * 忽略父factory和其他factory注册的单例bean
  7      * @param beanName the name of the bean to look for
  8      * @return if this bean factory contains a bean definition with the given name
  9      * @see #containsBean
 10      */
 11     boolean containsBeanDefinition(String beanName);
 12
 13     /**
 14      * factory中定义的bean数量
 15      * 一样不考虑父factory和其他factory注册的单例bean
 16      * @return the number of beans defined in the factory
 17      */
 18     int getBeanDefinitionCount();
 19
 20     /**
 21      * 获取工厂中定义的所有bean 的name
 22      * 一样不考虑父factory和其他factory注册的单例bean
 23      * @return the names of all beans defined in this factory,
 24      * or an empty array if none defined
 25      */
 26     String[] getBeanDefinitionNames();
 27
 28     /**
 29      * 获取给定类型的bean names(包括子类),通过bean 定义或者FactoryBean的getObjectType判断.
 30      * 注意:这个方法仅检查顶级bean.它不会检查嵌套的bean.
 31      * FactoryBean创建的bean会匹配为FactoryBean而不是原始类型.
 32      * 一样不会考虑父factory中的bean,可以使用BeanFactoryUtils中的beanNamesForTypeIncludingAncestors.
 33      * 其他方式注册的单例这边会纳入判断.
 34      * 这个版本的getBeanNamesForType会匹配所有类型的bean,包括单例,原型,FactoryBean.在大多数实现中返回结果跟getBeanNamesOfType(type,true,true)一样.
 35      * 返回的bean names会根据backend 配置的进行排序.
 36      * @param type the class or interface to match, or {@code null} for all bean names
 37      * @return the names of beans (or objects created by FactoryBeans) matching
 38      * the given object type (including subclasses), or an empty array if none
 39      * @see FactoryBean#getObjectType
 40      * @see BeanFactoryUtils#beanNamesForTypeIncludingAncestors(ListableBeanFactory, Class)
 41      */
 42     String[] getBeanNamesForType(Class<?> type);
 43
 44     /**
 45      *
 46      * @param type the class or interface to match, or {@code null} for all bean names
 47      * @param includeNonSingletons是否只要单例(包括BeanFactory),还是原型或其他作用域的bean一样包括
 48      * @param allowEagerInit 是否初始化懒加载的单例,FactoryBean初始化的类和工厂方法初始化的类.就是说执行这个方法会执行对应的初始化.
 49      * @return the names of beans (or objects created by FactoryBeans) matching
 50      * the given object type (including subclasses), or an empty array if none
 51      * @see FactoryBean#getObjectType
 52      * @see BeanFactoryUtils#beanNamesForTypeIncludingAncestors(ListableBeanFactory, Class, boolean, boolean)
 53      */
 54     String[] getBeanNamesForType(Class<?> type, boolean includeNonSingletons, boolean allowEagerInit);
 55
 56     /**
 57      *
 58      * @param type the class or interface to match, or {@code null} for all concrete beans
 59      * @return a Map with the matching beans, containing the bean names as
 60      * keys and the corresponding bean instances as values
 61      * @throws BeansException if a bean could not be created
 62      * @since 1.1.2
 63      * @see FactoryBean#getObjectType
 64      * @see BeanFactoryUtils#beansOfTypeIncludingAncestors(ListableBeanFactory, Class)
 65      */
 66     <T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException;
 67
 68     /**
 69      *
 70      * @param type the class or interface to match, or {@code null} for all concrete beans
 71      * @param includeNonSingletons whether to include prototype or scoped beans too
 72      * or just singletons (also applies to FactoryBeans)
 73      * @param allowEagerInit whether to initialize <i>lazy-init singletons</i> and
 74      * <i>objects created by FactoryBeans</i> (or by factory methods with a
 75      * "factory-bean" reference) for the type check. Note that FactoryBeans need to be
 76      * eagerly initialized to determine their type: So be aware that passing in "true"
 77      * for this flag will initialize FactoryBeans and "factory-bean" references.
 78      * @return a Map with the matching beans, containing the bean names as
 79      * keys and the corresponding bean instances as values
 80      * @throws BeansException if a bean could not be created
 81      * @see FactoryBean#getObjectType
 82      * @see BeanFactoryUtils#beansOfTypeIncludingAncestors(ListableBeanFactory, Class, boolean, boolean)
 83      */
 84     <T> Map<String, T> getBeansOfType(Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)
 85             throws BeansException;
 86
 87     /**
 88      * 找到使用注解的类.
 89      * @param annotationType the type of annotation to look for
 90      * @return a Map with the matching beans, containing the bean names as
 91      * keys and the corresponding bean instances as values
 92      * @throws BeansException if a bean could not be created
 93      */
 94     Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType)
 95             throws BeansException;
 96
 97     /**
 98      * 查找一个类上的注解,如果找不到,父类,接口使用注解也算.
 99      * @param beanName the name of the bean to look for annotations on
100      * @param annotationType the annotation class to look for
101      * @return the annotation of the given type found, or {@code null}
102      */
103     <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType);
104
105 }
时间: 2024-10-13 19:49:07

Spring源码解析 - ListableBeanFactory的相关文章

SPRING源码解析-SPRING 核心-IOC

IoC 和 AOP是Spring的核心, 是Spring系统中其他组件模块和应用开发的基础.透过这两个模块的设计和实现可以了解Spring倡导的对企业应用开发所应秉承的思路: 易用性. POJO开发企业应用, 直接依赖于Java语言,而不是容器和框架. 提升程序的可测试性,提高软件质量. 提供一致性编程模型,面向接口的编程 降低应用的负载和框架的侵入性.IoC和AOP实现. 不作为现有解决方案的替代,而是集成现有. IoC和AOP这两个核心组件,特别是IoC容器,使用户在使用Spring完成PO

Spring 源码解析之HandlerAdapter源码解析(二)

Spring 源码解析之HandlerAdapter源码解析(二) 前言 看这篇之前需要有Spring 源码解析之HandlerMapping源码解析(一)这篇的基础,这篇主要是把请求流程中的调用controller流程单独拿出来了 解决上篇文章遗留的问题 getHandler(processedRequest) 这个方法是如何查找到对应处理的HandlerExecutionChain和HandlerMapping的,比如说静态资源的处理和请求的处理肯定是不同的HandlerMapping ge

Spring 源码解析之ViewResolver源码解析(四)

Spring 源码解析之ViewResolver源码解析(四) 1 ViewResolver类功能解析 1.1 ViewResolver Interface to be implemented by objects that can resolve views by name. View state doesn't change during the running of the application, so implementations are free to cache views. I

软件开发工程师(JAVA)中级考试大纲--spring源码解析

spring源码解析(1)----IOC 一.IOC容器 在Spring中,IOC容器的重要地位我们就不多说了,对于Spring的使用者而言,IOC容器实际上是什么呢?我们可以说BeanFactory就是我们看到的IoC容器,当然了Spring为我们准备了许多种IoC容器来使用,这样可以方便我们从不同的层面,不同的资源位置,不同的形式的定义信息来建立我们需要的IoC容器. 在Spring中,最基本的IOC容器接口是BeanFactory - 这个接口为具体的IOC容器的实现作了最基本的功能规定 

Spring 源码解析之DispatcherServlet源码解析(五)

Spring 源码解析之DispatcherServlet源码解析(五) 前言 本文需要有前四篇文章的基础,才能够清晰易懂,有兴趣可以先看看详细的流程,这篇文章可以说是第一篇文章,也可以说是前四篇文章的的汇总,Spring的整个请求流程都是围绕着DispatcherServlet进行的 类结构图 根据类的结构来说DispatcherServlet本身也是继承了HttpServlet的,所有的请求都是根据这一个Servlet来进行转发的,同时解释了为什么需要在web.xml进行如下配置,因为Spr

Spring源码解析-applicationContext

Demo uml类图 ApplicationContext ApplicationListener 源码解析 主流程 obtainFreshBeanFactory prepareBeanFactory invokeBeanFactoryPostProcessors registerBeanPostProcessors registerListeners finishRefresh 总结 在已经有BeanFactory可以完成Ioc功能情况下,spring又提供了ApplicationContex

Spring源码解析和配置文件加载

Spring类的继承结构图: Spring运用了大量的模板方法模式和策略模式,所以各位看源码的时候,务必留意,每一个继承的层次都有不同的作用,然后将相同的地方抽取出来,依赖抽象将不同的处理按照不同的策略去处理. 步骤A. 读取 Resource 文件形成 Document 模型  类图: XmlBeanFactory -> XmlBeanDefinitionReader Spring 使用 XmlBeanDefinitionReader 来读取并解析 xml 文件,XmlBeanDefiniti

spring源码解析前瞻

IOC.AOP是spring的2个核心特性.理解这2个特性,有助于更好的解析源码. IOC:控制反转.把创建对象的权利交给框架,这有利于解耦. public class PageController { public String showPage(){ PageService page = new PageService(); return ""; } } 原先PageController中使用PageService,需要自己new创建对象,使用spring后,由容器创建PageSe

spring源码解析——spring源码导入eclipse

一.前言     众所周知,spring的强大之处.几乎所有的企业级开发中,都使用了spring了.在日常的开发中,我们是否只知道spring的配置,以及简单的使用场景.对其实现的代码没有进行深入的了解.开卷有益,在我们空闲的时间里面阅读一下spring的源码,对提升我们的自身能力还是还有很大的帮忙.下面总结一下spring源码导入eclipse的具体的操作. 二.spring的特点 spring的的核心就是IOC(控制反转)和AOP(基于切面的编程) 事务管理方面采用了:声明式事务 为各种主流