• SpringWeb项目获取所有访问路由


    背景

    公司APP经过多次迭代和长时间运行后,代码逐渐变得臃肿,为了便于代码瘦身和无用功能模块儿的剔除需要记录每个路由的访问次数
    
    • 1

    方案

    • 项目启动成功后获取路由并存入redis中
    • 通过拦截器获取路由地址进行计数

    相关代码

    启动后相关代码执行一次

    方法一:spring的ApplicationListener< ContextRefreshedEvent>接口(不推荐)

    实现ApplicationListener接口,并实现 onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent)方法

       	@Compent
       	public class RouterInit implements  ApplicationListener<ContextRefreshedEvent> {
       	   @Override
       	   public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
       	       if (contextRefreshedEvent.getApplicationContext().getParent() == null) {//保证只执行一次
       	           //需要执行的方法
       	       }
       	   }
       	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    方法2:springboot的ApplicationRunner接口

    ApplicationRunner和CommandLineRunner两个接口是springBoot提供用来在spring容器加载完成后执行指定方法。两个接口区别主要是入参不同。

    实现ApplicationRunner接口

    		
    @Component
    public class RouterInit implements CommandLineRunner {
       @Override
       public void run(String... args) throws Exception {
           System.out.println("执行方法");
       }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    方法3:springboot的CommandLineRunner接口

    实现CommandLineRunner接口

    @Component
    public class RouterInit implements CommandLineRunner {
        @Override
        public void run(String... args) throws Exception {
            System.out.println("执行方法");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    SpringApplication 的run方法会执行afterRefresh方法。
    afterRefresh方法会执行callRunners方法。
    callRunners方法会调用所有实现ApplicationRunner和CommondLineRunner接口的方法。

    获取路由逻辑

    @Slf4j
    @Component
    public class RouterRunner implements ApplicationRunner {
        @Autowired
        WebApplicationContext applicationContext;
    
        @Autowired
        RedisUtil redisUtil;
        @Override
        public void run(ApplicationArguments args) {
            RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
            // 获取url与类和方法的对应信息
            Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();
    
            List<Map<String, String>> list = new ArrayList<Map<String, String>>();
            for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {
                Map<String, String> map1 = new HashMap<String, String>();
                RequestMappingInfo info = m.getKey();
                HandlerMethod method = m.getValue();
                PatternsRequestCondition p = info.getPatternsCondition();
                for (String url : p.getPatterns()) {
                    map1.put("url", url);
                    Boolean hasKey = redisUtil.hHasKey(RedisConstant.ROUTER_STATISTICS_KEY, url);
                    if(!hasKey){
                        redisUtil.hset(RedisConstant.ROUTER_STATISTICS_KEY, url,0);
                    }
                }
                // 类名
                map1.put("className", method.getMethod().getDeclaringClass().getName());
                // 方法名
                map1.put("method", method.getMethod().getName());
                RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();
                for (RequestMethod requestMethod : methodsCondition.getMethods()) {
                    map1.put("type", requestMethod.toString());
                }
    
                list.add(map1);
            }
            System.err.println(JSON.toJSONString(list));
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    拦截逻辑

    /**
    * 请求拦截
    */
    @Slf4j
    @Component
    public class SessionInterceptor implements HandlerInterceptor {
    
       @Autowired
       private RedisUtil redisUtil;
    
       @Override
       public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) {
           String requestURI = request.getRequestURI();
           String path = requestURI.substring(request.getContextPath().length())
                   .replaceAll("[/]+$", "");
           //记录访问请求次数
           try {
               redisUtil.hincr(RedisConstant.ROUTER_STATISTICS_KEY, path, 1);
           } catch (Exception e) {
               log.error("路由计数失败", e);
           }
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
  • 相关阅读:
    【DAY11 软考中级备考笔记】数据结构 排序&&操作系统
    16-CSS3
    面试官:TCC解决方案是什么?如何解决TCC幂等问题?
    c++ condition_variable使用场景
    双非二本上岸滴滴暑期实习总结
    前端面试那些事【dt/dd、audio、onerror、标签、类、ID选择器、伪类选择器......
    JAVA集合04_Map接口概述、常用方法、排序、Hashtable面试题
    朗道-西格尔零点猜想(the Landau-Siegel Zeros Conjecture)
    nginx负载均衡
    海康Visionmaster-环境配置:VB.Net 二次开发环境配 置方法
  • 原文地址:https://blog.csdn.net/SheldonChang/article/details/126181367