当前位置:   article > 正文

spring 自定义注解(HandlerInterceptor实现)_handlerinterceptor 获取controller的class对象

handlerinterceptor 获取controller的class对象

spring 自定义注解(HandlerInterceptor实现)

      

         

                                

使用示例

             

自定义注解实现对controller层对标注了该注解的方法统计请求次数           

                   

        

CustomAnnotation

  1. @Documented
  2. @Retention(RetentionPolicy.RUNTIME)
  3. public @interface CustomAnnotation {
  4. }

       

CustomHandlerInterceptor

  1. @Component
  2. public class CustomHandlerInterceptor implements HandlerInterceptor {
  3. private Map<String,Integer> count = new HashMap<>();
  4. @Override
  5. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  6. HandlerMethod handlerMethod = (HandlerMethod) handler;
  7. Method method = handlerMethod.getMethod();
  8. if (method.isAnnotationPresent(CustomAnnotation.class)){
  9. String requestURI = request.getRequestURI();
  10. if (requestURI!=null || !"".equals(requestURI)){
  11. if (count.containsKey(requestURI)){
  12. count.put(requestURI,count.get(requestURI)+1);
  13. }else {
  14. count.put(requestURI,1);
  15. }
  16. }
  17. System.out.println(requestURI+"请求次数为:"+count.get(requestURI));
  18. }
  19. return HandlerInterceptor.super.preHandle(request, response, handler);
  20. }
  21. }

        

WebConfig

  1. @Configuration
  2. public class WebConfig implements WebMvcConfigurer {
  3. @Resource
  4. private CustomHandlerInterceptor customHandlerInterceptor;
  5. @Override
  6. public void addInterceptors(InterceptorRegistry registry) {
  7. registry.addInterceptor(customHandlerInterceptor).addPathPatterns("/**");
  8. }
  9. }

        

HelloController

  1. @RestController
  2. public class HelloController {
  3. @CustomAnnotation
  4. @RequestMapping("/hello")
  5. public String hello(){
  6. return "hello";
  7. }
  8. @RequestMapping("/hello2")
  9. public String hello2(){
  10. return "hello2";
  11. }
  12. }

       

         

                                

使用测试

      

localhost:8080/hello:连续点击两次,控制台输出

  1. 2022-04-25 13:46:12.268 INFO 1935 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 858 ms
  2. 2022-04-25 13:46:12.561 INFO 1935 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
  3. 2022-04-25 13:46:12.570 INFO 1935 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.489 seconds (JVM running for 2.096)
  4. 2022-04-25 13:46:16.786 INFO 1935 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
  5. 2022-04-25 13:46:16.786 INFO 1935 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
  6. 2022-04-25 13:46:16.787 INFO 1935 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 0 ms
  7. /hello请求次数为:1
  8. /hello请求次数为:2

       

localhost:8080/hello2:控制台无输出

      

             

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/240285
推荐阅读
相关标签
  

闽ICP备14008679号