如何详细探究Java中获取接口所有实现类的方法?
- 内容介绍
- 文章标签
- 相关推荐
本文共计614个文字,预计阅读时间需要3分钟。
有时,根据业务逻辑的需求,我们想要获取到某个接口的所有实现类。以下介绍两种方式:
1. 借助Spring容器实现 Spring可以作为容器,管理项目中的Java类。通过Spring,我们可以管理所有经过配置的Java类。具体操作如下:
- 在Spring配置文件中定义接口及其实现类的Bean。 - 在需要获取实现类的代码中,注入Spring容器。 - 通过容器获取接口的Bean列表,进而获取到所有实现类。
有时候,根据业务逻辑的需求,我们想要获取到某个接口的所有实现类。在这里大致介绍两种方式:
1.借助Spring容器实现
Spring作为一个容器,管理着一个项目中所有经过配置的Java类(xml配置文件或Annotation方式)。如果某个接口的所有实现类均被Spring托管了,那么通过Spring就可以很简单的返回这些实现类。
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class ServiceLocator implements ApplicationContextAware{ /** * 用于保存接口实现类名及对应的类 */ private Map<String, IService> map; /** * 获取应用上下文并获取相应的接口实现类 * @param applicationContext * @throws BeansException */ @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { //根据接口类型返回相应的所有bean Map<String, IService> map = applicationContext.getBeansOfType(IService.class); } public Map<String, IService> getMap() { return map; } }
2.借助ServiceLoader类
ServiceLoader是JDK自带的一个类加载器,位于java.util包当中,作为 A simple service-provider loading facility. 具体使用方式如下:
1.在META-INF/services/目录下用你的接口全路径名称命名一个文件(不加后缀),然后在该文件中一行一个添加你的接口实现类的全路径名。
2.通过load方法来加载出所有的接口实现类
ServiceLoader<MyInterface> loader = ServiceLoader.load(MyInterface.class);
在这里load方法的返回值是一个迭代器,用这个迭代器可以遍历出所有的接口实现类。
总结
以上两种方式,实现的功能都是一样的,实现方式不同,底层用的技术一样的,都是反射。至于选择哪一种,我建议如果项目中的接口实现类都被Spring托管了,那当然是直接用Spring了。如果没有用到Spring的话,那就用ServiceLoader,这个肯定是没有问题的。希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计614个文字,预计阅读时间需要3分钟。
有时,根据业务逻辑的需求,我们想要获取到某个接口的所有实现类。以下介绍两种方式:
1. 借助Spring容器实现 Spring可以作为容器,管理项目中的Java类。通过Spring,我们可以管理所有经过配置的Java类。具体操作如下:
- 在Spring配置文件中定义接口及其实现类的Bean。 - 在需要获取实现类的代码中,注入Spring容器。 - 通过容器获取接口的Bean列表,进而获取到所有实现类。
有时候,根据业务逻辑的需求,我们想要获取到某个接口的所有实现类。在这里大致介绍两种方式:
1.借助Spring容器实现
Spring作为一个容器,管理着一个项目中所有经过配置的Java类(xml配置文件或Annotation方式)。如果某个接口的所有实现类均被Spring托管了,那么通过Spring就可以很简单的返回这些实现类。
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class ServiceLocator implements ApplicationContextAware{ /** * 用于保存接口实现类名及对应的类 */ private Map<String, IService> map; /** * 获取应用上下文并获取相应的接口实现类 * @param applicationContext * @throws BeansException */ @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { //根据接口类型返回相应的所有bean Map<String, IService> map = applicationContext.getBeansOfType(IService.class); } public Map<String, IService> getMap() { return map; } }
2.借助ServiceLoader类
ServiceLoader是JDK自带的一个类加载器,位于java.util包当中,作为 A simple service-provider loading facility. 具体使用方式如下:
1.在META-INF/services/目录下用你的接口全路径名称命名一个文件(不加后缀),然后在该文件中一行一个添加你的接口实现类的全路径名。
2.通过load方法来加载出所有的接口实现类
ServiceLoader<MyInterface> loader = ServiceLoader.load(MyInterface.class);
在这里load方法的返回值是一个迭代器,用这个迭代器可以遍历出所有的接口实现类。
总结
以上两种方式,实现的功能都是一样的,实现方式不同,底层用的技术一样的,都是反射。至于选择哪一种,我建议如果项目中的接口实现类都被Spring托管了,那当然是直接用Spring了。如果没有用到Spring的话,那就用ServiceLoader,这个肯定是没有问题的。希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

