没事自己实现了个springmvc的东东~也不知道对不对~

简单实现了个类似@Controller和@GetMapping的东西...

1.自定义注解:

@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Rest {
    public String value()default "";
}

一个代替了类和方法的注解...都用一个.....

2.简单弄了个单例的容器,存储扫面的包路径

public class BasePackage {
    private static final List<String> list = new ArrayList<>();

    public static List<String> getList() {
        return list;
    }
}

3.写了个servlet的doGet实现:

@WebServlet("/*")
public class My extends HttpServlet {
    static List<Class> list = new ArrayList<>();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List urlList = get();
        String requestURI = req.getRequestURI();
        System.out.println(requestURI);
        if (urlList.isEmpty()) {
            throw new RuntimeException("list不能为空");
        }
        for (Class c : list) {
            Method[] declaredMethods = c.getDeclaredMethods();
            for (Method method : declaredMethods) {
                Rest annotation = method.getAnnotation(Rest.class);
                if (annotation != null) {
                    String value = annotation.value();
                    if (value.equals(requestURI)) {
                        String name = req.getParameter("name");
                        try {
                            Object invoke = method.invoke(c.newInstance(), name);
                            if (invoke != null) {
                                PrintWriter writer = resp.getWriter();
                                writer.write(invoke.toString());
                                writer.close();
                            }
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        } catch (InstantiationException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

    public List get() throws IOException {
        List<String> list1 = BasePackage.getList();
        if (list1.isEmpty()) {
            list1.add("web");
        }
        for (String s : list1) {
            Enumeration<URL> web = Thread.currentThread().getContextClassLoader().getResources(s);
            while (web.hasMoreElements()) {
                URL url = web.nextElement();
                String protocol = url.getProtocol();
                if (protocol.equals("file")) {
                    //获得包物理路径
                    String decode = URLDecoder.decode(url.getFile(), "utf-8");
                    DirectoryStream<Path> paths = Files.newDirectoryStream(Paths.get(decode.substring(1)));
                    for (Path path : paths) {
                        if (path.toString().endsWith(".class")) {
                            String className = s + "." + path.getFileName().toString().substring(0, path.getFileName().toString().indexOf(".class"));
                            try {
                                Class<?> aClass = Class.forName(className);
                                Rest declaredAnnotation = aClass.getDeclaredAnnotation(Rest.class);
                                if (declaredAnnotation != null) {
                                    list.add(aClass);
                                }
                            } catch (ClassNotFoundException e) {
                                e.printStackTrace();
                                System.out.println("加载类错误");
                            }
                        }
                    }
                }
            }
        }
        return list;
    }
}

通过反射做的,不过相对简单...参数也是获取固定,其实可以通过javassist这个工具获取参数名,来获取request参数;

4.测试类...

@Rest
public class MyWeb {

    @Rest("/test")
    public Object test(String name) {
        return "bbbb->>>" + name;
    }

    @Rest("/test2")
    public Object test2(String name) {
        return "aaaaa->>>" + name;
    }
}

哈哈,非常粗糙...

然后打成jar包后,程序初始化时给list赋值所要扫描的包路径即可....

猜你喜欢

转载自blog.csdn.net/myth_g/article/details/82756417
今日推荐