获取接口所有实现类的三种方式:Spring的ListableBeanFactory容器的getBeanNamesForType方法、利用Reflections工具进行反射扫描、使用SPI

前言

在策略模式应用中,需要获取到策略接口的所有实现类,本文记录三种获取某接口所有实现类的方法,分别是利用Spring的ListableBeanFactory容器的getBeanNamesForType方法,利用Reflections工具进行反射扫描、利用SPI方式。
在这里插入图片描述

Spring的ListableBeanFactory容器的getBeanNamesForType方法

如果所有实现类已被Spring托管,因此可以通过容器getBeanNamesForType方法获取接口的所有实现类。
如果实现类同时实现了FactoryBean接口的话,getBeanNamesForType从容器中拿到的实现类为FactoryBean自身,beanName带"&"前缀,而不是其getObject方法所代理的类。
获取到ListableBeanFactory容器,有如下方法:

  • 实现BeanFactoryPostProcessor接口
  • 实现ApplicationContextAware接口
	/**
	 * 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 whethe
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值