如何使用java代码加载指定包下面的所有类

来源:互联网 发布:nginx 开启hsts 编辑:程序博客网 时间:2024/05/18 00:38

如何加载指定包下面的所有类

使用代码来实现加载指定包下面的所有类

配置文件config.properties(其位置在resource目录下)

page=cn.ishow

读取配置文件工具类

/** * 读取配置文件工具类 */public abstract class ReadUtils {    private static Map<String,Object> cache = new HashMap<>();    private static Properties properties = null;    static {        properties = new Properties();        try {            properties.load(ReadUtils.class.getResourceAsStream("/config.properties"));        } catch (IOException e) {            e.printStackTrace();        }    }    public static Object read(String key){        Object value = cache.get(key);        if(value==null){            synchronized (ReadUtils.class){                value = cache.get(key);                if(value==null) {                    value = properties.get(key);                    cache.put(key, value);                }            }        }        return value;    }}

主要类

package cn.ishow.manage.test;import java.io.File;import java.util.ArrayList;import java.util.List;public class LoadPageClassTest {    /**     * 解析包的全路径(比你的包路径为cn.ishow.test)     * @param webPackage     * @return     */    public String resovlePackagePath(String webPackage) {        // 扫码所有的包并把其放入到访问关系和方法放入到内存中        File f = new File(getClass().getResource("/").getPath());        String totalPath = f.getAbsolutePath();        System.out.println(totalPath);        String pageName = getClass().getPackage().getName().replace(".", "\\");        totalPath = totalPath.replace(pageName, "");        String packagePath = webPackage.replace(".", "\\");        totalPath = totalPath + "\\" + packagePath;        return totalPath;    }    /**     * 解析包下面的所有类     * @param packagePath 上一步获取包的全路径     * @param webPackage  包(cn.ishow.test)     * @return     */    public List<String> parseClassName(String packagePath, String webPackage) {        List<String> array = new ArrayList<>();        File root = new File(packagePath);        resolveFile(root, webPackage, array);        return array;    }    private void resolveFile(File root, String webPackage, List<String> classNames) {        if (!root.exists())            return;        File[] childs = root.listFiles();        if (childs != null && childs.length > 0) {            for (File child : childs) {                if (child.isDirectory()) {                    String parentPath = child.getParent();                    String childPath = child.getAbsolutePath();                    String temp = childPath.replace(parentPath, "");                    temp = temp.replace("\\", "");                    resolveFile(child, webPackage + "." + temp, classNames);                } else {                    String fileName = child.getName();                    if (fileName.endsWith(".class")) {                        String name = fileName.replace(".class", "");                        String className = webPackage + "." + name;                        classNames.add(className);                    }                }            }        }    }    public static void main(String[] args) {        LoadPageClassTest test = new LoadPageClassTest();        String packageData = (String) ReadUtils.read("page");        String totalPath = test.resovlePackagePath(packageData);        System.out.println(packageData +"   "+totalPath);        List<String> datas = test.parseClassName(totalPath,packageData);        System.out.println(datas);    }}

结果

[cn.ishow.manage.mapper.ITestEntityMapper, cn.ishow.manage.mongo.dao.IBaseDao, cn.ishow.manage.mongo.dao.impl.BaseDaoImpl, cn.ishow.manage.mongo.dao.impl.TestBeanDaoImpl, cn.ishow.manage.mongo.dao.impl.UserBeanDaoImpl, cn.ishow.manage.mongo.dao.ITestBeanDao, cn.ishow.manage.mongo.dao.IUserBeanDao, cn.ishow.manage.redis.RedisCache, cn.ishow.manage.service.impl.TestBeanServiceImpl, cn.ishow.manage.service.impl.TestEntityServiceImpl, cn.ishow.manage.service.impl.UserBeanServiceImpl, cn.ishow.manage.test.LoadPageClassTest, cn.ishow.manage.test.ReadUtils, cn.ishow.manage.weixing.WeiXingUtils]

有了这些类全名称,我们就可以利用反射获取该类,并进行一系列操作。由于本人有点忙,很多人用csdn提问我都没回,如果有问题qq发消息本人qq863224759。下篇将写本人简单模拟springmvc的实现

阅读全文
0 0
原创粉丝点击