ListableBeanFactory接口

在这里插入图片描述
ListableBeanFactory获取bean时,Spring 鼓励使用这个接口定义的api. 还有个Beanfactory方便使用.其他的4个接口都是不鼓励使用的.

提供容器中bean迭代的功能,不再需要一个个bean地查找.比如可以一次获取全部的bean(太暴力了),根据类型获取bean.在看SpringMVC时,扫描包路径下的具体实现策略就是使用的这种方式(那边使用的是BeanFactoryUtils封装的api).

如果同时实现了HierarchicalBeanFactory,返回值不会考虑父类BeanFactory,只考虑当前factory定义的类.当然也可以使用BeanFactoryUtils辅助类来查找祖先工厂中的类. 即ListableBeanFactory是beanFactory接口的扩展接口,它可以枚举所有的bean实例,而不是客户端通过名称一个一个的查询得出所有的实例。要预加载所有的bean定义的beanfactory可以实现这个接口来。该 接口定义了访问容器中Bean基本信息的若干方法,如查看Bean的个数、获取某一类型Bean的配置名、查看容器中是否包括某一Bean等方法.

这个接口中的方法只会考虑本factory定义的bean.这些方法会忽略ConfigurableBeanFactory的registerSingleton注册的单例bean(getBeanNamesOfType和getBeansOfType是例外,一样会考虑手动注册的单例).当然BeanFactory的getBean一样可以透明访问这些特殊bean.当然在典型情况下,所有的bean都是由external bean定义,所以应用不需要顾虑这些差别.

注意:getBeanDefinitionCount和containsBeanDefinition的实现方法因为效率比较低,还是少用为好.
ListableBeanFactory源码具体:

1、3个跟BeanDefinition有关的总体操作。包括BeanDefinition的总数、名字的集合、指定类型的名字的集合。(这里指出,BeanDefinition是Spring中非常重要的一个类,每个BeanDefinition实例都包含一个类在Spring工厂中所有属性。)

2、2个getBeanNamesForType重载方法。根据指定类型(包括子类)获取其对应的所有Bean名字。

3、2个getBeansOfType重载方法。根据类型(包括子类)返回指定Bean名和Bean的Map。

4、2个跟注解查找有关的方法。根据注解类型,查找Bean名和Bean的Map。以及根据指定Bean名和注解类型查找指定的Bean。

总结:

正如这个工厂接口的名字所示,这个工厂接口最大的特点就是可以列出工厂可以生产的所有实例。当然,工厂并没有直接提供返回所有实例的方法,也没这个必要。它可以返回指定类型的所有的实例。而且你可以通过getBeanDefinitionNames()得到工厂所有bean的名字,然后根据这些名字得到所有的Bean。这个工厂接口扩展了BeanFactory的功能,作为上文指出的BeanFactory二级接口,有9个独有的方法,扩展了跟BeanDefinition的功能,提供了BeanDefinition、BeanName、注解有关的各种操作。它可以根据条件返回Bean的集合,这就是它名字的由来——ListableBeanFactory。

/*** * @author DemoTransfer* @since 4.3*/
public interface ListableBeanFactory extends BeanFactory {//-------------------------------------------------------------------------// 暴力获取全部bean的属性//-------------------------------------------------------------------------/*** Check if this bean factory contains a bean definition with the given name.* <p>Does not consider any hierarchy this factory may participate in,* and ignores any singleton beans that have been registered by* other means than bean definitions.* @param beanName the name of the bean to look for* @return if this bean factory contains a bean definition with the given name* @see #containsBean*/// 对于给定的名字是否含有BeanDefinitionboolean containsBeanDefinition(String beanName);/*** Return the number of beans defined in the factory.* <p>Does not consider any hierarchy this factory may participate in,* and ignores any singleton beans that have been registered by* other means than bean definitions.* @return the number of beans defined in the factory*/// 返回工厂的BeanDefinition总数int getBeanDefinitionCount();/*** Return the names of all beans defined in this factory.* <p>Does not consider any hierarchy this factory may participate in,* and ignores any singleton beans that have been registered by* other means than bean definitions.* @return the names of all beans defined in this factory,* or an empty array if none defined*/// 返回工厂中所有Bean的名字String[] getBeanDefinitionNames();//-------------------------------------------------------------------------// 根据bean 的类型获取bean//// 这边的方法仅检查顶级bean.它不会检查嵌套的bean.FactoryBean创建的bean会匹配为FactoryBean而不是原始类型.// 一样不会考虑父factory中的bean,非要用可以通过BeanFactoryUtils中的beanNamesForTypeIncludingAncestors.// 其他方式注册的单例这边会纳入判断.// 这个版本的getBeanNamesForType会匹配所有类型的bean,包括单例,原型,FactoryBean.返回的bean names会根据backend 配置的进行排序.//-------------------------------------------------------------------------/*** Return the names of beans matching the given type (including subclasses),* judging from either bean definitions or the value of {@code getObjectType}* in the case of FactoryBeans.* <p><b>NOTE: This method introspects top-level beans only.</b> It does <i>not</i>* check nested beans which might match the specified type as well.* <p>Does consider objects created by FactoryBeans, which means that FactoryBeans* will get initialized. If the object created by the FactoryBean doesn't match,* the raw FactoryBean itself will be matched against the type.* <p>Does not consider any hierarchy this factory may participate in.* Use BeanFactoryUtils' {@code beanNamesForTypeIncludingAncestors}* to include beans in ancestor factories too.* <p>Note: Does <i>not</i> ignore singleton beans that have been registered* by other means than bean definitions.* <p>This version of {@code getBeanNamesForType} matches all kinds of beans,* be it singletons, prototypes, or FactoryBeans. In most implementations, the* result will be the same as for {@code getBeanNamesForType(type, true, true)}.* <p>Bean names returned by this method should always return bean names <i>in the* order of definition</i> in the backend configuration, as far as possible.* @param type the class or interface to match, or {@code null} for all bean names* @return the names of beans (or objects created by FactoryBeans) matching* the given object type (including subclasses), or an empty array if none* @since 4.2* @see #isTypeMatch(String, ResolvableType)* @see FactoryBean#getObjectType* @see BeanFactoryUtils#beanNamesForTypeIncludingAncestors(ListableBeanFactory, ResolvableType)*/String[] getBeanNamesForType(ResolvableType type);/*** Return the names of beans matching the given type (including subclasses),* judging from either bean definitions or the value of {@code getObjectType}* in the case of FactoryBeans.* <p><b>NOTE: This method introspects top-level beans only.</b> It does <i>not</i>* check nested beans which might match the specified type as well.* <p>Does consider objects created by FactoryBeans, which means that FactoryBeans* will get initialized. If the object created by the FactoryBean doesn't match,* the raw FactoryBean itself will be matched against the type.* <p>Does not consider any hierarchy this factory may participate in.* Use BeanFactoryUtils' {@code beanNamesForTypeIncludingAncestors}* to include beans in ancestor factories too.* <p>Note: Does <i>not</i> ignore singleton beans that have been registered* by other means than bean definitions.* <p>This version of {@code getBeanNamesForType} matches all kinds of beans,* be it singletons, prototypes, or FactoryBeans. In most implementations, the* result will be the same as for {@code getBeanNamesForType(type, true, true)}.* <p>Bean names returned by this method should always return bean names <i>in the* order of definition</i> in the backend configuration, as far as possible.* @param type the class or interface to match, or {@code null} for all bean names* @return the names of beans (or objects created by FactoryBeans) matching* the given object type (including subclasses), or an empty array if none* @see FactoryBean#getObjectType* @see BeanFactoryUtils#beanNamesForTypeIncludingAncestors(ListableBeanFactory, Class)*/// 获取给定类型的bean names(包括子类),通过bean 定义或者FactoryBean的getObjectType判断.// 返回对于指定类型Bean(包括子类)的所有名字String[] getBeanNamesForType(Class<?> type);/*** Return the names of beans matching the given type (including subclasses),* judging from either bean definitions or the value of {@code getObjectType}* in the case of FactoryBeans.* <p><b>NOTE: This method introspects top-level beans only.</b> It does <i>not</i>* check nested beans which might match the specified type as well.* <p>Does consider objects created by FactoryBeans if the "allowEagerInit" flag is set,* which means that FactoryBeans will get initialized. If the object created by the* FactoryBean doesn't match, the raw FactoryBean itself will be matched against the* type. If "allowEagerInit" is not set, only raw FactoryBeans will be checked* (which doesn't require initialization of each FactoryBean).* <p>Does not consider any hierarchy this factory may participate in.* Use BeanFactoryUtils' {@code beanNamesForTypeIncludingAncestors}* to include beans in ancestor factories too.* <p>Note: Does <i>not</i> ignore singleton beans that have been registered* by other means than bean definitions.* <p>Bean names returned by this method should always return bean names <i>in the* order of definition</i> in the backend configuration, as far as possible.* @param type the class or interface to match, or {@code null} for all bean names* @param includeNonSingletons whether to include prototype or scoped beans too* or just singletons (also applies to FactoryBeans)* @param allowEagerInit whether to initialize <i>lazy-init singletons</i> and* <i>objects created by FactoryBeans</i> (or by factory methods with a* "factory-bean" reference) for the type check. Note that FactoryBeans need to be* eagerly initialized to determine their type: So be aware that passing in "true"* for this flag will initialize FactoryBeans and "factory-bean" references.* @return the names of beans (or objects created by FactoryBeans) matching* the given object type (including subclasses), or an empty array if none* @see FactoryBean#getObjectType* @see BeanFactoryUtils#beanNamesForTypeIncludingAncestors(ListableBeanFactory, Class, boolean, boolean)*//** 返回指定类型的名字 includeNonSingletons为false表示只取单例Bean,true则不是* allowEagerInit为true表示立刻加载,false表示延迟加载。 注意:FactoryBeans都是立刻加载的。*/String[] getBeanNamesForType(Class<?> type, boolean includeNonSingletons, boolean allowEagerInit);/*** Return the bean instances that match the given object type (including* subclasses), judging from either bean definitions or the value of* {@code getObjectType} in the case of FactoryBeans.* <p><b>NOTE: This method introspects top-level beans only.</b> It does <i>not</i>* check nested beans which might match the specified type as well.* <p>Does consider objects created by FactoryBeans, which means that FactoryBeans* will get initialized. If the object created by the FactoryBean doesn't match,* the raw FactoryBean itself will be matched against the type.* <p>Does not consider any hierarchy this factory may participate in.* Use BeanFactoryUtils' {@code beansOfTypeIncludingAncestors}* to include beans in ancestor factories too.* <p>Note: Does <i>not</i> ignore singleton beans that have been registered* by other means than bean definitions.* <p>This version of getBeansOfType matches all kinds of beans, be it* singletons, prototypes, or FactoryBeans. In most implementations, the* result will be the same as for {@code getBeansOfType(type, true, true)}.* <p>The Map returned by this method should always return bean names and* corresponding bean instances <i>in the order of definition</i> in the* backend configuration, as far as possible.* @param type the class or interface to match, or {@code null} for all concrete beans* @return a Map with the matching beans, containing the bean names as* keys and the corresponding bean instances as values* @throws BeansException if a bean could not be created* @since 1.1.2* @see FactoryBean#getObjectType* @see BeanFactoryUtils#beansOfTypeIncludingAncestors(ListableBeanFactory, Class)*/// 如果保护懒加载的类,FactoryBean初始化的类和工厂方法初始化的类会被初始化.就是说执行这个方法会执行对应的初始化.// 根据类型(包括子类)返回指定Bean名和Bean的Map<T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException;/*** Return the bean instances that match the given object type (including* subclasses), judging from either bean definitions or the value of* {@code getObjectType} in the case of FactoryBeans.* <p><b>NOTE: This method introspects top-level beans only.</b> It does <i>not</i>* check nested beans which might match the specified type as well.* <p>Does consider objects created by FactoryBeans if the "allowEagerInit" flag is set,* which means that FactoryBeans will get initialized. If the object created by the* FactoryBean doesn't match, the raw FactoryBean itself will be matched against the* type. If "allowEagerInit" is not set, only raw FactoryBeans will be checked* (which doesn't require initialization of each FactoryBean).* <p>Does not consider any hierarchy this factory may participate in.* Use BeanFactoryUtils' {@code beansOfTypeIncludingAncestors}* to include beans in ancestor factories too.* <p>Note: Does <i>not</i> ignore singleton beans that have been registered* by other means than bean definitions.* <p>The Map returned by this method should always return bean names and* corresponding bean instances <i>in the order of definition</i> in the* backend configuration, as far as possible.* @param type the class or interface to match, or {@code null} for all concrete beans* @param includeNonSingletons whether to include prototype or scoped beans too* or just singletons (also applies to FactoryBeans)* @param allowEagerInit whether to initialize <i>lazy-init singletons</i> and* <i>objects created by FactoryBeans</i> (or by factory methods with a* "factory-bean" reference) for the type check. Note that FactoryBeans need to be* eagerly initialized to determine their type: So be aware that passing in "true"* for this flag will initialize FactoryBeans and "factory-bean" references.* @return a Map with the matching beans, containing the bean names as* keys and the corresponding bean instances as values* @throws BeansException if a bean could not be created* @see FactoryBean#getObjectType* @see BeanFactoryUtils#beansOfTypeIncludingAncestors(ListableBeanFactory, Class, boolean, boolean)*/<T> Map<String, T> getBeansOfType(Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)throws BeansException;//-------------------------------------------------------------------------// 查找使用注解的类//-------------------------------------------------------------------------/*** Find all names of beans whose {@code Class} has the supplied {@link Annotation}* type, without creating any bean instances yet.* @param annotationType the type of annotation to look for* @return the names of all matching beans* @since 4.0*/String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType);/*** Find all beans whose {@code Class} has the supplied {@link Annotation} type,* returning a Map of bean names with corresponding bean instances.* @param annotationType the type of annotation to look for* @return a Map with the matching beans, containing the bean names as* keys and the corresponding bean instances as values* @throws BeansException if a bean could not be created* @since 3.0*/// 根据注解类型,查找所有有这个注解的Bean名和Bean的MapMap<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) throws BeansException;//-------------------------------------------------------------------------// 查找一个类上的注解,如果找不到,父类,接口使用注解也算.//-------------------------------------------------------------------------/*** Find an {@link Annotation} of {@code annotationType} on the specified* bean, traversing its interfaces and super classes if no annotation can be* found on the given class itself.* @param beanName the name of the bean to look for annotations on* @param annotationType the annotation class to look for* @return the annotation of the given type if found, or {@code null}* @throws NoSuchBeanDefinitionException if there is no bean with the given name* @since 3.0*/// 根据指定Bean名和注解类型查找指定的Bean<A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)throws NoSuchBeanDefinitionException;}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/249522.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

面向对象之三大特性:继承,封装,多态

python面向对象的三大特性&#xff1a;继承&#xff0c;封装&#xff0c;多态。 1. 封装: 把很多数据封装到⼀个对象中. 把固定功能的代码封装到⼀个代码块, 函数, 对象, 打包成模块. 这都属于封装的思想. 具体的情况具体分析. 比如. 你写了⼀个很⽜B的函数. 那这个也可以被称为…

configurablebeanfactory

ConfigurableBeanFactory定义BeanFactory的配置.ConfigurableBeanFactory中定义了太多太多的api,比如类加载器,类型转化,属性编辑器,BeanPostProcessor,作用域,bean定义,处理bean依赖关系,合并其他ConfigurableBeanFactory,bean如何销毁. ConfigurableBeanFactory同时继承了Hi…

外观模式

一、什么是外观模式   有些人可能炒过股票&#xff0c;但其实大部分人都不太懂&#xff0c;这种没有足够了解证券知识的情况下做股票是很容易亏钱的&#xff0c;刚开始炒股肯定都会想&#xff0c;如果有个懂行的帮帮手就好&#xff0c;其实基金就是个好帮手&#xff0c;支付宝…

OC内存管理

OC内存管理 一、基本原理 &#xff08;一&#xff09;为什么要进行内存管理。 由于移动设备的内存极其有限&#xff0c;所以每个APP所占的内存也是有限制的&#xff0c;当app所占用的内存较多时&#xff0c;系统就会发出内存警告&#xff0c;这时需要回收一些不需要再继续使用的…

面试题集锦

1. L1范式和L2范式的区别 (1) L1范式是对应参数向量绝对值之和 (2) L1范式具有稀疏性 (3) L1范式可以用来作为特征选择&#xff0c;并且可解释性较强&#xff08;这里的原理是在实际Loss function 中都需要求最小值&#xff0c;根据L1的定义可知L1最小值只有0&#xff0c;故可以…

Spring注解配置工作原理源码解析

一、背景知识 在【Spring实战】Spring容器初始化完成后执行初始化数据方法一文中说要分析其实现原理&#xff0c;于是就从源码中寻找答案&#xff0c;看源码容易跑偏&#xff0c;因此应当有个主线&#xff0c;或者带着问题、目标去看&#xff0c;这样才能最大限度的提升自身代…

Spring--Context

应用上下文 Spring通过应用上下文&#xff08;Application Context&#xff09;装载bean的定义并把它们组装起来。Spring应用上下文全权负责对象的创建和组装。Spring自带了多种应用上下文的实现&#xff0c;它们之间主要的区别仅仅在于如何加载配置。 1.AnnotationConfigApp…

了解PID控制

2019-03-07 【小记】 了解PID控制 比例 - 积分 - 微分 积分 --- 记忆过去 比例 --- 了解现在 微分 --- 预测未来 转载于:https://www.cnblogs.com/skullboyer/p/10487884.html

program collections

Java byte & 0xff byte[] b new byte[1];b[0] -127;System.out.println("b[0]:"b[0]"; b[0]&0xff:"(b[0] & 0xff));//output:b[0]:-127; b[0]&0xff:129计算机内二进制都是补码形式存储&#xff1a; b[0]: 补码&#xff0c;10000001&…

Spring ConfigurationClassPostProcessor Bean解析及自注册过程

一bean的自注册过程 二,自注册过程说明 1 configurationclassparser解析流程 1、处理PropertySources注解&#xff0c;配置信息的解析 2、处理ComponentScan注解&#xff1a;使用ComponentScanAnnotationParser扫描basePackage下的需要解析的类(SpringBootApplication注解也包…

2019第二周作业

基础作业 实验代码 #include<stdlib.h> int main(void) {FILE*fp;int num[4],i,b,max;char op;if((fpfopen("c:\\tmj.txt","r"))NULL){ printf("File open error!\n"); exit(0);}for(i0;i<4;i){fscanf(fp,"%d%c",&nu…

实验一(高见老师收)

学 号201521450016 中国人民公安大学 Chinese people’ public security university 网络对抗技术 实验报告 实验一 网络侦查与网络扫描 学生姓名 陈璪琛 年级 2015 区队 五 指导教师 高见 信息技术与网络安全学院 2018年9月18日 实验任务总纲 2018—2019学年…

Spring 钩子之BeanFactoryPostProcessor和BeanPostProcessor

BeanFactoryPostProcessor和BeanPostProcessor这两个接口都是初始化bean时对外暴露的入口之一&#xff0c;和Aware类似&#xff08;PS:关于spring的hook可以看看Spring钩子方法和钩子接口的使用详解讲的蛮详细&#xff09;本文也主要是学习具体的钩子的细节&#xff0c;以便于实…

什么是HTML DOM对象

HTML DOM 对象 HTML DOM Document 对象 Document 对象 每个载入浏览器的 HTML 文档都会成为 Document 对象。 Document 对象使我们可以从脚本中对 HTML 页面中的所有元素进行访问。 提示&#xff1a;Document 对象是 Window 对象的一部分&#xff0c;可通过 window.document 属…

Python3 matplotlib的绘图函数subplot()简介

Python3 matplotlib的绘图函数subplot()简介 一、简介 matplotlib下, 一个 Figure 对象可以包含多个子图(Axes), 可以使用 subplot() 快速绘制, 其调用形式如下 : subplot(numRows, numCols, plotNum) 图表的整个绘图区域被分成 numRows 行和 numCols 列 然后按照从左到右&…

signal(SIGHUP, SIG_IGN);

signal(SIGHUP, SIG_IGN); 的理解转载于:https://www.cnblogs.com/lanjiangzhou/p/10505653.html

spring钩子

Spring钩子方法和钩子接口的使用详解 前言 SpringFramework其实具有很高的扩展性&#xff0c;只是很少人喜欢挖掘那些扩展点&#xff0c;而且官方的Refrence也很少提到那些Hook类或Hook接口&#xff0c;至于是不是Spring官方有意为之就不得而知。本文浅析一下笔者目前看到的S…

day 012 生成器 与 列表推导式

生成器的本质就是迭代器&#xff0c;写法和迭代器不一样&#xff0c;用法一样。 获取方法&#xff1a; 1、通过生成器函数 2、通过各种推导式来实现生成器 3、通过数据的转换也可以获取生成器 例如&#xff1a; 更改return 为 yield 即成为生成器 该函数就成为了一个生成器函数…

20172325 2018-2019-1 《Java程序设计》第二周学习总结

20172325 2018-2019-1 《Java程序设计》第二周学习总结 教材学习内容总结 3.1集合 集合是一种聚集、组织了其他对象的对象。集合可以分为两大类&#xff1a;线性集合和非线性集合。线性集合&#xff1a;一种其元素按照直线方式组织的集合。非线性集合&#xff1a;一种其元素按某…

题解 luogu P2568 GCD

题解 luogu P2568 GCD 时间&#xff1a;2019.3.11 欧拉函数前缀和 题目描述 给定整数\(N\)&#xff0c;求\(1\le x,y \le N\)且\(\gcd(x,y)\)为素数的数对\((x,y)\)有多少对. 分析 枚举素数\(p\), 先求出\(1\le x,y \le \left \lfloor \dfrac n p \right \rfloor\)且\(\gcd(x, …