模块功能

http://localhost:8080/car/get访问网页拦截器计算时间


结构
  1. java.cn.tedu
  2. controller//包
  3. controller//类
  4. MyConfig//类
  5. MyInterlnterceptor//类
  6. RunApp
  7. pom.xml

RunApp
  1. package cn.tedu;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.stereotype.Controller;
  5. @SpringBootApplication
  6. @Controller
  7. public class RunApp {
  8. public static void main(String[] args) {
  9. SpringApplication.run(RunApp.class);
  10. }
  11. }

测试CarController

  1. package cn.tedu.controller;
  2. import cn.tedu.RunApp;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. @RestController
  7. @RequestMapping("/car")
  8. public class CarController {
  9. @RequestMapping("/save")
  10. public String save( ) {
  11. System.out.println("save方法");
  12. return "save方法";
  13. }
  14. @RequestMapping("/get")
  15. public String get() {
  16. for (int i = 0; i < 10; i++) {
  17. System.out.println("get执行中");
  18. }
  19. return "get方法";
  20. }
  21. }

MyInterlnterceptor创建拦截器
  1. package cn.tedu.controller;
  2. import org.springframework.stereotype.Component;
  3. import org.springframework.web.servlet.HandlerInterceptor;
  4. import org.springframework.web.servlet.ModelAndView;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import javax.xml.ws.handler.Handler;
  8. @Component
  9. public class MyInterlnterceptor implements HandlerInterceptor {
  10. long start;
  11. @Override
  12. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  13. start= System.currentTimeMillis();
  14. return true;
  15. }
  16. @Override
  17. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
  18. long end = System.currentTimeMillis();
  19. System.out.println("方法执行了"+(end-start)+"ms");
  20. }
  21. @Override
  22. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
  23. System.out.println("资源释放完成");
  24. }
  25. }

MyConfig注册拦截器
  1. package cn.tedu.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  5. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  6. @Configuration//告诉spring容器,我是一个配置类
  7. public class MyConfig implements WebMvcConfigurer {
  8. @Autowired//自动装配
  9. private MyInterlnterceptor my;
  10. @Override
  11. public void addInterceptors(InterceptorRegistry registry) {
  12. //把自定义的拦截器,放入容器中
  13. registry.addInterceptor(my);
  14. }
  15. }

image.png