获取所有springmvc中注解RequestMapping

思路是检测ClassLoader中所有已加载的类,遍历是否有RequestMapping注解,对存在的逐个打印出来

 @Test
    public void annotationScan() throws IllegalAccessException {
        Field field = null;
        try {
            field = ClassLoader.class.getDeclaredField("classes");
            field.setAccessible(true);
            Vector<Class> classes=(Vector<Class>) field.get(this.getClass().getClassLoader());
            List<Class> cl=new ArrayList<>(classes);

            Iterator<Class> itor=cl.iterator();
            while(itor.hasNext()){
                Class c=itor.next();
                if(c.getAnnotation(RequestMapping.class)!=null){
                    RequestMapping req= (RequestMapping)c.getAnnotation(RequestMapping.class);
                    String[] bath=req.path().length>0?req.path():req.value();
                    if(bath.length==0){
                        continue ;
                    }
                    Method[] ms=c.getDeclaredMethods();
                    for(Method m : ms){
                        RequestMapping rm=m.getAnnotation(RequestMapping.class);
                        if(rm==null){
                            continue ;
                        }
                        String[] bath2=rm.path().length>0?rm.path():rm.value();
                        if(bath2.length==0){
                            continue ;
                        }
                        System.out.println(bath[0]+bath2[0]);
                    }
                }
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }


猜你喜欢

转载自blog.csdn.net/qq631431929/article/details/51320508