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,一经查实,立即删除!

相关文章

HDU 4035 Maze

Maze http://acm.hdu.edu.cn/showproblem.php?pid4035 分析&#xff1a; 在树上走来走去&#xff0c;然后在一个点可以k的概率回到1&#xff0c;可以e的概率走出去&#xff0c;可以1-k-e的概率走到其他的位置&#xff08;分为父节点和子节点讨论&#xff09;。 转移方程就是&a…

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

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

configurablebeanfactory

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

Xlua文件在热更新中调用方法

Xlua文件在热更新中调用方法 public class news : MonoBehaviour { LuaEnv luaEnv;//定义Lua初始变量 void Awake() { luaEnv new LuaEnv();//new开辟空间 luaEnv.AddLoader(myload);//调用方法地址、返回字节 luaEnv.DoString("requirefish");//更新文件 } void O…

springboot 使用的配置

1&#xff0c;控制台打印sql logging:level:com.sdyy.test.mapper: debug 2&#xff0c;开启驼峰命名 mybatis.configuration.map-underscore-to-camel-casetrue 转载于:https://www.cnblogs.com/xiaohu1218/p/10477318.html

AutowireCapableBeanFactory接口

AutowireCapableBeanFactory在BeanFactory基础上实现了对存在实例的管理.可以使用这个接口集成其它框架,捆绑并填充并不由Spring管理生命周期并已存在的实例.像集成WebWork的Actions 和Tapestry Page就很实用. 一般应用开发者不会使用这个接口,所以像ApplicationContext这样的…

外观模式

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

OC内存管理

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

cf1132E. Knapsack(搜索)

题意 题目链接 Sol 看了status里面最短的代码。。感觉自己真是菜的一批。。直接爆搜居然可以过&#xff1f;。。但是现在还没终测所以可能会fst。。 #include<bits/stdc.h> #define Pair pair<int, int> #define MP(x, y) make_pair(x, y) #define fi first #defi…

ConfigurableListableBeanFactory

ConfigurableListableBeanFactory 提供bean definition的解析,注册功能,再对单例来个预加载(解决循环依赖问题). 貌似我们一般开发就会直接定义这么个接口了事.而不是像Spring这样先根据使用情况细分那么多,到这边再合并 ConfigurableListableBeanFactory具体&#xff1a; 1、…

焦旭超 201771010109《面向对象程序设计课程学习进度条》

《2018面向对象程序设计&#xff08;java&#xff09;课程学习进度条》 周次 &#xff08;阅读/编写&#xff09;代码行数 发布博客量/博客评论量 课堂/课余学习时间&#xff08;小时&#xff09; 最满意的编程任务 第一周 50/20 1/0 6/4 九九乘法表 第二周 90/5…

面试题集锦

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;这样才能最大限度的提升自身代…

halt

关机 init 0 reboot init6 shutdown -r now 重启 -h now 关机 转载于:https://www.cnblogs.com/todayORtomorrow/p/10486123.html

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&…

软件测试问题

1.什么是兼容性测试?兼容性测试侧重哪些方面? 主要检验的是软件的可移植性&#xff0c;检查软件在不同的硬件平台软件平台上是否可以正常的运行。 细分会有&#xff1a;平台的兼容&#xff0c;网络兼容&#xff0c;数据库兼容&#xff0c;数据格式的兼容等。 2.常用的测试方法…

Spring注解源码分析

我们知道如果想使用spring注解你需要在applicationContext.xml配置文件中设置context:component-scan base-packagexxx’这样spring会帮助我们扫描你所设置的目录里面所有的Bean&#xff0c;如果Bean上面有相应的Service,Controller注解&#xff08;当然还有其他的&#xff0c;…

linux查看和修改PATH环境变量的方法

查看PATH&#xff1a;echo $PATH以添加mongodb server为列修改方法一&#xff1a;export PATH/usr/local/mongodb/bin:$PATH//配置完后可以通过echo $PATH查看配置结果。生效方法&#xff1a;立即生效有效期限&#xff1a;临时改变&#xff0c;只能在当前的终端窗口中有效&…