zoukankan      html  css  js  c++  java
  • JAVA在不确定具体 Annotation 类型时,获得注解参数

      
    package com.lzw.demo;
    
    @SpringBootApplication
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
        @RestController
        @RequestMapping(path = "/a")
        public static class A {
            @GetMapping(path = "/find")
            public void add() {
                System.out.println("find");
            }
            @DeleteMapping(path = "/delete")
            public void delete() {
                System.out.println("delete");
            }
        }
    
        @RestController
        @RequestMapping(path = "/b")
        public static class B {
            @PutMapping(path = "/update")
            public void update() {
                System.out.println("update");
            }
            @PostMapping(path = "/add")
            public void add() {
                System.out.println("add");
            }
        }
    
        /**
         * Created by laizhenwei on 2018-1-29 22:48:49.
         */
        @Component
        public static class appStartUp implements CommandLineRunner {
    
            @Autowired
            private RequestMappingHandlerMapping requestMappingHandlerMapping;
    
            @Override
            public void run(String... strings){
                Class<Annotation>[] annoClz =
                        new Class[]{RequestMapping.class, PostMapping.class,
                                GetMapping.class, DeleteMapping.class, PutMapping.class};
                Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();
                List<Class<?>> beanTypes = map.keySet()
                        .stream().filter(info -> map.get(info)
                                .getBeanType().getAnnotation(RequestMapping.class) != null)
                        .collect(Collectors.toList())
                        .stream().map(i -> map.get(i).getBeanType())
                        .collect(Collectors.toList());
                beanTypes.forEach(t -> Arrays.stream(t.getMethods()).forEach(method ->
                        {
                            for (int i = 0; i < annoClz.length; i++) {
                                Annotation mappingAnno = method.getAnnotation(annoClz[i]);
                                if (mappingAnno != null) {
                                    try {
                                        InvocationHandler invocationHandler = Proxy.getInvocationHandler(mappingAnno);
                                        Field value = invocationHandler.getClass().getDeclaredField("memberValues");
                                        value.setAccessible(true);
                                        Map<String, Object> memberValues = (Map<String, Object>) value.get(invocationHandler);
                                        System.out.println(Arrays.toString(((String[]) memberValues.get("path"))));
                                    } catch (Exception ex) {
                                        ex.printStackTrace();
                                    }
                                }
                            }
                        }
                ));
            }
        }
    }

  • 相关阅读:
    洛谷P3224 [HNOI2012]永无乡 线段树合并
    洛谷P3605 [USACO17JAN]Promotion Counting——线段树合并
    python之三元表达式、列表推导、生成器表达式、递归、匿名函数、内置函数
    python之迭代器、生成器与面向过程编程
    Python之函数对象、函数嵌套、名称空间与作用域、闭包函数、装饰器
    Python之函数基础
    Python之字符编码与文件操作
    Python基本数据类型
    python介绍
    编程基础
  • 原文地址:https://www.cnblogs.com/sweetchildomine/p/8379838.html
Copyright © 2011-2022 走看看