获取Java接口的所有实现类

前言:想看基于spring 的最简单实现方法,请直接看 第七步。

本文价值在于 包扫描的原理探究和实现

一、背景

项目开发中,使用Netty做服务端,保持长连接与客户端(agent)通讯。Netty服务端需要根据不同消息类型,加载对应的Processer(消息处理器)对消息进行处理。问题就出现了,Processer会随着消息业务类型增多进行扩展,每一次增加Processer都需要手动new出来一个实例,放到Map里(key为消息类型码,value为Processer实例),供调度程序(ProcesserManager)根据端消息类型调度,显然这是件很麻烦的一件事,不仅操作琐碎,也不符合低耦合、模块化的设计思想。

二、解决思路

我们所写的每一个Processer都是IProcessor这个接口的实现:

public interface IProcessor {

    void process(BaseMsgWrapper msg) throws Exception;

    EventEnum getType();

    default String getIpFromChannelContext(ChannelHandlerContext ctx){
        String[] ipPort = ctx.channel().remoteAddress().toString().split(":");
        return ipPort[0].substring(1);
    }
}

其中:

void process(BaseMsgWrapper msg)  为消息处理方法

void getIpFromChannelContext (BaseMsgWrapper msg)  为获取客户端ip的默认方法

假如我们在Netty服务端启动时,能获取该接口的所有实现类,然后把这些实现类分别new出来,放到Map中,那么这个工作就可以自动化掉了。

最终实现的效果就是 消息处理器只要 implements IProcessor接口,就会被自动加载调用,而不再需要手动写到Map中。这样就将ProcesserManager 与 Processer解耦开了。

为此,IProcessor接口需要增加一个方法

EventEnum getType();
    即需要Processer表明自己对应的消息类型,没这个方法之前,我们都是在put进Map的时候,手动把消息类型写进去的(可以想象之前的做法多么的low)


三、实现过程

想法是很好,但实现不是那么容易,踩了很多坑。

首先是网上查资料,看看其他人都怎么做的,有没有做好的轮子。

第一篇博客参考:http://www.cnblogs.com/ClassNotFoundException/p/6831577.html

(Java -- 获取指定接口的所有实现类或获取指定类的所有继承类)

这篇博客提供的大致思路:

1) 获取当前线程的ClassLoader

2) 通过ClassLoader获取当前工作目录,对目录下的文件进行遍历扫描。

3) 过滤出以.class为后缀的类文件,并加载类到list中

4) 对list中所有类进行校验,判断是否为指定接口的实现类,并排除自身。

5) 返回所有符合条件的类。

这个思路是对的,但是考虑不全,不能拿来工程应用,另外博文中提供的源码应该只是一个实验代码,有不少缺陷。

1)这个方没有考虑不同的文件格式。当程序打成jar包,发布运行时,上述的这种遍历file的操作 就失效了。

2)局限性。只能扫描到当前方法的同级目录及其子目录。无法覆盖整个模块。

3)遍历文件的逻辑太啰嗦,可以简化。

4)通过ClassLoader获取当前工作目录时,使用了“../bin/”这么一个固定的目录名。

Enumeration<URL> enumeration = classLoader.getResources("../bin/" + path)

事实上,不同的IDE(主要是eclipse 和 idea)项目的资源目录,在这一点上是不同的。

第二篇博客参考:

http://blog.csdn.net/littleschemer/article/details/47378455

(获取全部子类或接口的全部实现)

这篇博客考虑到了在运行环境中,需要通过JarFile工具类进行单独处理。

局限性:

需要手动指定要扫描的Jar文件或目录,没有通过ClassLoader 自动获取当前运行的上下文。

此外classLoader.getResource 获得的 资源目录 是个URL对象,如何转换成JarFile对象 花费了我不少时间求索:

JarURLConnection jarURLConnection = (JarURLConnection)url.openConnection();
   JarFile
jarFile = jarURLConnection.getJarFile();

综合上述思路和自己的试验研究,得出获取接口所有实现类的算法流程如下:

四、代码实现

package com.hikvision.hummer.pandora.gateway.proc;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.net.JarURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 *
获取接口的所有实现类 理论上也可以用来获取类的所有子类
 
* 查询路径有限制,只局限于接口所在模块下,比如pandora-gateway,而非整个pandora(会递归搜索该文件夹下所以的实现类)
 
* 路径中不可含中文,否则会异常。若要支持中文路径,需对该模块代码中url.getPath() 返回值进行urldecode.
 * Created by wangzhen3 on 2017/6/23.
 */
public class ClassUtil {
    private static
final Logger LOG = LoggerFactory.getLogger(ClassUtil.class);

public static ArrayList<Class> getAllClassByInterface(Class clazz) {
        ArrayList<Class> list = new ArrayList<>();
        // 判断是否是一个接口
        if (clazz.isInterface()) {
            try {
                ArrayList<Class>
allClass = getAllClass(clazz.getPackage().getName());
                /**
                 *
循环判断路径下的所有类是否实现了指定的接口 并且排除接口类自己
                
*/
               
for (int i = 0; i < allClass.size(); i++) {
                    /**
                     *
判断是不是同一个接口
                    
*/
                   
// isAssignableFrom:判定此 Class 对象所表示的类或接口与指定的 Class
                    // 参数所表示的类或接口是否相同,或是否是其超类或超接口
                    if (clazz.isAssignableFrom(allClass.get(i))) {
                        if (!clazz.equals(allClass.get(i))) {
                            // 自身并不加进去
                            list.add(allClass.get(i));
                        }
                    }
                }
            } catch (Exception e) {
                LOG.error("出现异常{}",e.getMessage());
                throw new RuntimeException("出现异常"+e.getMessage());
            }
        }
        LOG.info("class list size :"+list.size());
        return list;
    }

/**
     *
从一个指定路径下查找所有的类
    
*
     * @param
packagename
    
*/
   
private static ArrayList<Class> getAllClass(String
packagename) {

LOG.info("packageName to search:"+packagename);
       List<String>
classNameList =  getClassName(packagename);
        ArrayList<Class>
list = new ArrayList<>();

for(String
className : classNameList){
            try {
                list.add(Class.forName(className));
            } catch (ClassNotFoundException e) {
                LOG.error("load class from name failed:"+className+e.getMessage());
                throw new RuntimeException("load class from name failed:"+className+e.getMessage());
            }
        }
        LOG.info("find list size :"+list.size());
        return list;
    }

/**
     *
获取某包下所有类
    
* @param packageName 包名
    
* @return 类的完整名称
    
*/
   
public static List<String> getClassName(String
packageName) {

List<String> fileNames = null;
        ClassLoader
loader = Thread.currentThread().getContextClassLoader();
        String
packagePath = packageName.replace(".", "/");
        URL url =
loader.getResource(packagePath);
        if (url != null) {
            String type =
url.getProtocol();
            LOG.debug("file type : " + type);
            if (type.equals("file")) {
                String fileSearchPath =
url.getPath();
                LOG.debug("fileSearchPath: "+fileSearchPath);
                fileSearchPath =
fileSearchPath.substring(0,fileSearchPath.indexOf("/classes"));
                LOG.debug("fileSearchPath: "+fileSearchPath);
                fileNames = getClassNameByFile(fileSearchPath);
            } else if (type.equals("jar")) {
                try{
                    JarURLConnection
jarURLConnection = (JarURLConnection)url.openConnection();
                    JarFile jarFile = jarURLConnection.getJarFile();
                    fileNames = getClassNameByJar(jarFile,packagePath);
                }catch (java.io.IOException e){
                    throw new RuntimeException("open
Package URL failed:"+e.getMessage());
                }

}else{
                throw new RuntimeException("file system not support! cannot
load MsgProcessor!");
            }
        }
        return fileNames;
    }

/**
     *
从项目文件获取某包下所有类
    
* @param filePath 文件路径
    
* @return 类的完整名称
    
*/
   
private static List<String> getClassNameByFile(String filePath) {
        List<String> myClassName = new ArrayList<String>();
        File file = new File(filePath);
        File[]
childFiles = file.listFiles();
        for (File childFile
: childFiles) {
            if (childFile.isDirectory()) {
                myClassName.addAll(getClassNameByFile(childFile.getPath()));
            } else {
                String childFilePath =
childFile.getPath();
                if (childFilePath.endsWith(".class")) {
                    childFilePath =
childFilePath.substring(childFilePath.indexOf("\\classes") + 9, childFilePath.lastIndexOf("."));
                    childFilePath = childFilePath.replace("\\", ".");
                    myClassName.add(childFilePath);
                }
            }
        }

return myClassName;
    }

/**
     *
jar获取某包下所有类
    
* @return 类的完整名称
    
*/
   
private static List<String> getClassNameByJar(JarFile jarFile ,String
packagePath) {
        List<String> myClassName = new ArrayList<String>();
        try {
            Enumeration<JarEntry>
entrys = jarFile.entries();
            while (entrys.hasMoreElements()) {
                JarEntry jarEntry =
entrys.nextElement();
                String entryName
= jarEntry.getName();
                //LOG.info("entrys
jarfile:"+entryName);
                if (entryName.endsWith(".class")) {
                    entryName =
entryName.replace("/", ".").substring(0, entryName.lastIndexOf("."));
                    myClassName.add(entryName);
                    //LOG.debug("Find Class :"+entryName);
                }
            }
        } catch (Exception e) {
            LOG.error("发生异常:"+e.getMessage());
            throw new RuntimeException("发生异常:"+e.getMessage());
        }
        return myClassName;
    }

}

五、项目应用

接口IProcessor

*/
public interface IProcessor {

void process(BaseMsgWrapper msg) throws Exception;

EventEnum getType();

default String getIpFromChannelContext(ChannelHandlerContext ctx){
        String[] ipPort =
ctx.channel().remoteAddress().toString().split(":");
        return ipPort[0].substring(1);
    }
}

接口实现类HeartBeatMsgProcessor,
主要 加了注解@Component

@Component
public class HeartBeatMsgProcessor implements IProcessor {

private static
final HikGaLogger logger =
HikGaLoggerFactory.getLogger(HeartBeatMsgProcessor.class);

@Override
    public EventEnum getType(){
        return EventEnum.HEART_BEAT;
    }

private BaseMsg bmsg = new BaseMsg( "requestId-null", EventEnum.HEART_BEAT.getEventType(),1L, Constants.ASYN_INVOKE,
            "pong", "uuid-null", Constants.ZH_CN);

@Override
    public void process(BaseMsgWrapper msg) throws Exception {
        Assert.notNull(msg);
        logger.debug("ping from [{}]", msg.getCtx().channel().remoteAddress().toString());
        msg.getCtx().writeAndFlush(bmsg);
    }
}

调用ClassUtil 获取接口的所有类,并根据查找到的类从spring容器中取出bean.

private ProcessorManager(){
    List<Class> classList = ClassUtil.getAllClassByInterface(IProcessor.class);
    LOG.info("processor num :"+classList.size());
    for(Class classItem : classList){
        IProcessor msgProcessor = null;
        try{
            msgProcessor =  (IProcessor) AppContext.getBean(classItem);
            processorMap.put(msgProcessor.getType(),msgProcessor);
        }catch (Exception e){
            LOG.error("加载脚本处理器:[{}]失败:[{}]!",classItem.getName(),e.getMessage());
            throw new RuntimeException("加载脚本处理器"+classItem.getName()+"失败");
        }
        LOG.info("加载脚本处理器成功:[{}] MsgType:[{}] ", classItem.getName(), msgProcessor.getType());
    }
    LOG.info("脚本处理器加载完成!");
}

代码中AppContext是对springContext 的封装,实现了ApplicationContextAware接口,目的是从Spring容器取出指定类的实例。代码见附录1.

 

六、更进一步

本文通过研究Java接口实现类的自动扫描加载,达成接口与调度程序的解耦,拓展了Java接口在代码解耦方面的应用价值。

虽然是获取接口所有实现类,但对获取类的所有子类,同样适用。

另外基于此功能,可以通过反射分析扫描上来的Class, 获知哪些类使用了自定义注解,然后应用注解处理器,完成注解处理器的自动执行。

 

七、最简单的实现方法

ApplicationContext 的 getBeansOfType 方法已经封装了该实现,可以直接调用。

AppContext 见附录1

IProcessor 为接口。Map<String, IProcessor> processorBeanMap 为返回值,key 为beanName ,value为 bean.

接口IProcessor实现类上 要加上注解@Component

Map<String, IProcessor> processorBeanMap = null;
try {
    processorBeanMap = AppContext.getContext().getBeansOfType(IProcessor.class);
}catch (BeansException e){
    throw new RuntimeException("加载脚本处理器Bean失败!");
}

调用AppContext 时,AppContex 必须已经被容器优先注入,否则可能会出现applicaitonContext未注入的报错。

可以在调用类上,加上注解
@DependsOn("appContext") 来控制appContext
优先加载。

附录1 AppContext

package com.hikvision.hummer.pandora.common.context;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class AppContext implements ApplicationContextAware {     private static ApplicationContext context = null;
    /**
     *
实现ApplicationContextAware接口的context注入函数, 将其存入静态变量
    
*/
   
public void setApplicationContext(ApplicationContext context) {
        AppContext.setContext(context);
    }     /**
     *
取得存储在静态变量中的ApplicationContext.
     */
   
public static ApplicationContext getContext() {
        if (context == null)
            throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义AppContext");
        return context;
    }
    /**
     *
存储静态变量中的ApplicationContext.
     */
   
public static void setContext(ApplicationContext context) {
        AppContext.context = context;
    }
    /**
     *
从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型
    
*/
   
@SuppressWarnings("unchecked")
    public static <T> T getBean(String name) {
        if (context == null)
            throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义AppContext");
        try {
            return (T) context.getBean(name);
        } catch (BeansException e) {
            e.printStackTrace();
        }
        return (T) null;
    }     /**
     *
从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型
    
*/
   
@SuppressWarnings("unchecked")
    public static <T> T getBean(Class<T> tClass) {
        if (context == null)
            throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义AppContext");
        try {
            return context.getBean(tClass);
        } catch (BeansException e) {
            e.printStackTrace();
        }
        return null;
    }
}

获取Java接口的所有实现类的更多相关文章

  1. Java -- 获取指定接口的所有实现类或获取指定类的所有继承类

    Class : ClassUtil package pri.lime.main; import java.io.File; import java.io.IOException; import jav ...

  2. 简述java接口和C++虚类的相同和不同之处

    C++虚类相当于java中的抽象类,与接口的不同处是: 1.一个子类只能继承一个抽象类(虚类),但能实现多个接口 2.一个抽象类可以有构造方法,接口没有构造方法 3.一个抽象类中的方法不一定是抽象方法 ...

  3. C# 获取指定接口的所有实现类

    var types = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(a => a.GetTypes().Where(t => t ...

  4. 获取同一接口多个实现类的bean

    @Service("taskExecutorFactory") public class TaskExecutorFactory implements ApplicationCon ...

  5. Android系统进程间通信Binder机制在应用程序框架层的Java接口源代码分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6642463 在前面几篇文章中,我们详细介绍了A ...

  6. java接口和抽象类

    关于接口 1.创建一个接口,需要使用interface关键字. 2.实现一个接口,需要使用implements关键字. 3.接口的成员属性都是静态常量(默认public static final). ...

  7. Java 接口和抽象类差别

    原文:http://blog.csdn.net/sunboard/article/details/3831823 1.概述 一个软件设计的好坏,我想非常大程度上取决于它的总体架构,而这个总体架构事实上 ...

  8. 我对面向对象设计的理解——Java接口和Java抽象类

    在没有好好地研习面向对象设计的设计模式之前,我对Java接口和Java抽象类的认识还是很模糊,很不可理解. 刚学Java语言时,就很难理解为什么要有接口这个概念,虽说是可以实现所谓的多继承,可一个只有 ...

  9. Java 接口和抽象类区别(写的很好,转了)

    原文:http://blog.csdn.net/sunboard/article/details/3831823 1.概述 一个软件设计的好坏,我想很大程度上取决于它的整体架构,而这个整体架构其实就是 ...

随机推荐

  1. 「LuoguP3865」 【模板】ST表 (线段树

    题目背景 这是一道ST表经典题——静态区间最大值 请注意最大数据时限只有0.8s,数据强度不低,请务必保证你的每次查询复杂度为 O(1) 题目描述 给定一个长度为 N 的数列,和 M 次询问,求出每一 ...

  2. 开启sqlplus中执行计划

    在sqlplus中我们一般用Autotrace来查看执行计划,从而对于一些语句执行过程分析,开展优化工作.这里就演示一下如何将autotrace权限授予给普通的用户,以scott用户为例(set au ...

  3. Code-NFine:变量修改

    ylbtech-Code-NFine:Cookie变量修改 1. NFine.Code返回顶部 1.Operator 1.1.OperatorProvider.cs /**************** ...

  4. 收藏产品判断、html 在 UIwebView里面显示

     收藏产品功能 要求:用户点击收藏,如果已经收藏,用户点击就取消收藏 写法一: 点击事件{ if (!isSelect) { [sender setImage:[UIImage imageNamed: ...

  5. Collection View Programming Guide for iOS---(三)---Designing Your Data Source and Delegate

      Designing Your Data Source and Delegate 设计你的数据源和委托 Every collection view must have a data source o ...

  6. 游戏服务端pomelo安装配置

    一.安装环境 Linux Ubantu 二.安装需要的组件 1.安装nodejs 注:debian下nodejs没有相应的apt包,所以无法用apt-get安装,只能通过nodejs的源码包安装, 这 ...

  7. csacademy Round #36(模拟+最坏情况)

    传送门 题意 给出n种袜子,每种袜子个数a[i],两只相同种类袜子配成一对,询问至少拿出多少只袜子能确保配出k对袜子 分析 In order to find out the minimum numbe ...

  8. IDEA thymeleaf ${xxx.xxx}表达式报错,红色波浪线

    解决方法: 在<!DOCTYPE html>标签下面加上 <!--suppress ALL--> 代码如图: 不再报错,效果如下图:

  9. 用jquery的animate动画函数做的网页效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 微信BUG之微信内置的浏览器中window.location.href 不跳转

    最近做微信开发遇到这个问题,查了一些文档,总结一下 1.url后面加参数 indow.location.href = url +'?timestamp='+ new Date().getTime()+ ...