in tomcat,there are two projects under webapps , A and B 。 In project A ,I want to get annotations on class in B,the same code ,I can get annotations in common java project,but in springmvc,I cannot get any annotation. why?
here are the core codes
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;
}}
here is the maintest
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);
});
}}
I can get the annotations enter image description here
however , I cannot get any annotation when deploy in 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;
}