package com.mycompany.mavenspringmvc.mvc.util;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @ClassNmae:ScannerUtil
* @Description:扫描工具类,将指定的类加载进内存操作
* @author zlx-雄雄
* @date 2017-8-14 20:33:45
*
* SpringMvc框架
* 1,mvc框架 Module View Controller
* 2,包扫描类加载
* 3,管理起来,然后通过注解判断哪些是控制器(Controller RequestMapping)
* 4,做好路径映射,DispatcheServlet做控制器转发解析r
*/
public class ScannerUtil {
/*扫描包com.mycompany.mavenspringmvc.mvc*/
public static void main(String[] args) {
//配置文件中配置包名
scannerClass("com.mycompany.mavenspringmvc.controller");
}
/**
* @param packagePath
* @return Map<String,Class<?>> 类名与类实例的集合
*/
public static Map<String,Class<?>> scannerClass(String packagePath){
//怎么把包转换为路径com.mycompany.mavenspringmvc.mvc com/mycompany/mavenspringmvc/mvc
String filePath = packagePath.replace(".", "/");
System.out.println("包名替换后的路径"+filePath);
Map<String,Class<?>> classes = new HashMap<>();
try {
//取得路径的枚举对象
Enumeration<URL> dirs = Thread.currentThread().getContextClassLoader().getResources(filePath);
//取得根目录,完整的包名路径,物理路径
String root = Thread.currentThread().getContextClassLoader().getResource(filePath).getPath();
/**
* System.out.println(root);
* /F:/SVNFile/mavenSpringMVC/target/classes/com/mycompany/mavenspringmvc/controller
*/
if(root!=null){
//其实这个路径也就是包名根路径开始到指定包名的路径
root = root.substring(root.lastIndexOf(filePath));
System.out.println(root.endsWith(filePath));
}
//迭代url对象
while (dirs.hasMoreElements()) {
URL url = dirs.nextElement();//拿到当前迭代的url元素
/**
* System.out.println(url);
* file:/F:/SVNFile/mavenSpringMVC/target/classes/com/mycompany/mavenspringmvc/controller
* file:/文件协议
*/
//通过url就可以判断出是文件还是文件夹
if(url.getProtocol().equals("file")){
/**
* 通过url对象产生实际的文件对象
* substring(1),文件路径前面有个“/”
*/
File folder = new File(url.getPath().substring(1));
//开始扫描文件夹,子文件夹所有的类
scannerFile(folder,root,classes);
}
}
} catch (IOException ex) {
Logger.getLogger(ScannerUtil.class.getName()).log(Level.SEVERE, null, ex);
}
return classes;
}
/**
* 将文件夹下面所有的类文件扫描出来
* 遇到子文件夹是递归调用
*/
public static void scannerFile(File folder,String root,Map<String ,Class<?>> classes) {
//获取文件夹里面的所有文件对象
File[] files = folder.listFiles();
for (File file : files) {
//判断这个文件对象是不是文件夹,是文件夹递归,不是文件夹就得到文件对象
if(file.isDirectory()){
//递归扫描子文件夹
scannerFile(file, root+file.getName()+"/", classes);
}else{//如果是文件,有可能是class文件
String path = file.getAbsolutePath();//物理路径
if(path.endsWith(".class")){
path = path.replace("\\", "/");
/**
* path = com/mycompany/mavenspringmvc/controller/IndexController.class
* path.lastIndexOf("/")最后一个“/”,需要加1,去掉“/”
*/
String className = root+path.substring(path.lastIndexOf("/")+1, path.indexOf(".class"));
//得到完整的包名
className = className.replace("/", ".");
try {
classes.put(className, Class.forName(className));
} catch (ClassNotFoundException ex) {
Logger.getLogger(ScannerUtil.class.getName()).log(Level.SEVERE, null, ex);
}
System.err.println(className);
}
}
}
}
}