在tomcat中,如何在其他项目的类中获取注释

时间:2018-06-22 02:06:11

标签: java urlclassloader

在tomcat中,webapps下有两个项目,A和B。在项目A中,我想在B中的类上获得批注,相同的代码,我可以在常见的Java项目中获得批注,但是在springmvc中,我不能得到任何注释。为什么?

这是核心代码

public class Recursion {
/**
 * 
 *
 * @param path
 * @param filePaths
 * @param fileType
 */

public static void searchFiles(String path, List<String> filePaths, String fileType) {
    File file = new File(path);
    if (file.isFile()) {
        filePaths.add(path);
        return;
    }
    File[] files = file.listFiles();
    if (files != null) {
        for (File itemFile : files) {
            if (itemFile.isFile() && itemFile.getName().endsWith(fileType)) {
                filePaths.add(itemFile.getAbsolutePath());
            } else if (itemFile.isDirectory()) {
                searchFiles(itemFile.getAbsolutePath(), filePaths, fileType);
            }
        }
    }
}


/**
 * 
 *
 * @param path
 * @return
 */

public static ConcurrentHashMap<String, String> getClasses(String path) {
    List<String> filePaths = new ArrayList<>();
    searchFiles(path, filePaths, ".class");
    ConcurrentHashMap<String, String> classs = new ConcurrentHashMap<>();
    for (String filePath : filePaths) {
        int classes = filePath.indexOf("classes");
        String packageName = filePath.substring(classes + 8, filePath.length());
        packageName = packageName.replaceAll("\\\\", ".");
        packageName = packageName.replaceAll(".class", "");
        classs.put(packageName, filePath);
    }
    return classs;
}}



public class ClassLoaderUtil extends ClassLoader {
private byte[] results;

public ClassLoaderUtil(String classPathName) {
    results = loadClassFile(classPathName);
}


/**
 * @param packageName 
 */
public void getAnnotations(String packageName) {
    try {
        Class<?> clazz = this.loadClass(packageName);
        Object object = clazz.newInstance();
        Annotation[] annotations = object.getClass().getAnnotations();
        System.out.println(object.getClass().getName());
        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        for (Annotation annotation : annotations) {
            System.out.println(annotation.toString());
        }
        System.out.println("");
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

/**
 *
 *
 * @param classPath
 * @return
 */
private byte[] loadClassFile(String classPath) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
        FileInputStream fi = new FileInputStream(classPath);
        BufferedInputStream bis = new BufferedInputStream(fi);
        byte[] data = new byte[1024 * 256];
        int ch = 0;
        while ((ch = bis.read(data, 0, data.length)) != -1) {
            bos.write(data, 0, ch);
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return bos.toByteArray();

}

@Override
protected Class<?> loadClass(String arg0, boolean arg1) {
    Class<?> clazz = findLoadedClass(arg0);
    if (clazz == null) {
        if (getParent() != null) {
            try {
                clazz = getParent().loadClass(arg0);
            } catch (Exception e) {
            }
        }
        if (clazz == null) {
            clazz = defineClass(arg0, results, 0, results.length);
        }
    }
    return clazz;
}}

这是最主要的

public class test {
public static void main(String[] args) {
    String webBaseRealPath = "D:\\myself\\apache-tomcat-8.5.28\\webapps\\http\\WEB-INF\\classes\\com\\sac\\web\\controller";
    ConcurrentHashMap<String, String> classes = Recursion.getClasses(webBaseRealPath);
    classes.forEach((k, v) -> {
        ClassLoaderUtil myClassLoader = new ClassLoaderUtil(v);
        myClassLoader.getAnnotations(k);
    });
}}

我可以得到注释 enter image description here

但是,在tomcat中部署时,我无法获得任何注释

@RequiresRoles({"dev"})
@ApiOperation(notes = "GET", httpMethod = "GET", value = "获取系统全部资源列表")
@GetMapping(value = "resources/total")
public ResponseEntity getAll(HttpServletRequest servletRequest) throws ClassNotFoundException {
    String webBaseRealPath = servletRequest.getServletContext().getRealPath("");
    String webBusinessRelativePath = ConfigurationProvider.config.getWebCore().getPublishPath().getBussinessApiRelativePath();
    String webBaseRelativePath = ConfigurationProvider.config.getWebCore().getPublishPath().getApiRelativePath();
    String webBusinessRealPath = webBaseRealPath.replace(webBaseRelativePath, webBusinessRelativePath);
    //test
    webBaseRealPath = "D:\\myself\\apache-tomcat-8.5.28\\webapps\\http\\WEB-INF\\classes\\com\\sac\\web\\controller";
    ConcurrentHashMap<String, String> classes = Recursion.getClasses(webBaseRealPath);
    classes.clear();
    classes.put("com.sac.web.controller.BaseController",
            "D:\\myself\\apache-tomcat-8.5.28\\webapps\\http\\WEB-INF\\classes\\com\\sac\\web\\controller\\BaseController.class");
    classes.forEach((k, v) -> {
        ClassLoaderUtil myClassLoader = new ClassLoaderUtil(v);
        myClassLoader.getAnnotations(k);
    });
    //Class<?> aClass = Class.forName("com.sac.web.controller.OrganizationController");
    resourceService.getAllSystemResources(webBaseRealPath);
    return null;
}

0 个答案:

没有答案