基本拦截器
控制器
@RequestMapping(value = "/member_add_pre", method = RequestMethod.GET)
public String memberAddPre() {
return "member_add"; // 此处只返回一个路径, 该路径没有设置后缀,后缀默认是*.html
}
@PostMapping(value = "/member_add")
@ResponseBody
public Object member(Member member){
return member;
}
MyInterceptor 配置拦截器
public class MyInterceptor implements HandlerInterceptor {
private Logger log =LoggerFactory.getLogger(MyInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
HandlerMethod handlerMethod =(HandlerMethod) handler;
this.log.info("---------进入拦截器preHandle-----------------"+handlerMethod.getBean().getClass().getName());
return true; //如果返回false表示不继续请求,如果返回true表示继续进行请求
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
HandlerMethod handlerMethod =(HandlerMethod) handler;
this.log.info("---------进入拦截器postHandle-----------------"+handlerMethod.getBean().getClass().getName());
this.log.info("---------进入拦截器postHandle-----------------"+modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
this.log.info("---------拦截处理完毕afterCompletion---------");
}
}
注册拦截器规则
@Configuration
public class MyWebApplicationConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
}
/**
*@author 安儿
* 自定义拦截器注入Bean
*/
@Bean
public MyInterceptor MyInterceptor() {
return new MyInterceptor();
}
}
基础拦截器的实现结果 :
boot.utils.MyInterceptor : ---------进入拦截器preHandle-----------------org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController
boot.utils.MyInterceptor : ---------进入拦截器postHandle-----------------org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController
boot.utils.MyInterceptor : ---------进入拦截器postHandle-----------------null
boot.utils.MyInterceptor : ---------拦截处理完毕afterCompletion---------
AOP 方式配置拦截器
aop 服务层
public interface IMemberService {
public Member get(long mid);
}
aop 服务实现层
@Service
public class MemberServiceImpl implements IMemberService {
@Override
public Member get(long mid) {
Member vo =new Member();
vo.setMid(mid);
vo.setName("KING");
vo.setSalaryl(5000.00);
return vo;
}
}
aop Controller 控制层
@RequestMapping(value = "/member_get",method = RequestMethod.GET)
@ResponseBody
public Object get(long mid){
return this.memberService.get(mid);
}
aop 拦截器实现结果
o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
o.s.web.servlet.DispatcherServlet : Completed initialization in 6 ms
boot.utils.MyInterceptor : ---------进入拦截器preHandle-----------------boot.controller.MemberController
boot.config.ServiceAspect : [----------Server—Before -------]执行参数:[1]
boot.config.ServiceAspect : [----------Server—agter -------]执行参数:boot.vo.Member@25518404
boot.utils.MyInterceptor : ---------进入拦截器postHandle-----------------boot.controller.MemberController
boot.utils.MyInterceptor : ---------进入拦截器postHandle-----------------null
boot.utils.MyInterceptor : ---------拦截处理完毕afterCompletion---------