SpringBoot拦截器配置
自定义拦截器:
com.example.mybatis2018.interceptor.MyInterCeptor
package com.example.mybatis2018.interceptor;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
/**
* 自定义拦截器
*/
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyInterCeptor implements HandlerInterceptor{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("拦截器执行");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
}
}
1. 注解版配置:
创建配置类
com.example.mybatis2018.config.MyMvcConfig
package com.example.mybatis2018.config;
import com.example.mybatis2018.interceptor.MyInterCeptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class MyMvcConfig extends WebMvcConfigurationSupport{
@Override
protected void addInterceptors(InterceptorRegistry registry) {
//注册自己的拦截器,并设置拦截路径,拦截多个可以全一个list集合
registry.addInterceptor(new MyInterCeptor()).addPathPatterns("/");
}
}
2. 配置文件版 (官方不推荐使用):
定义配置文件
interceptor.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!--配置拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/"/>
<bean class="com.example.mybatis2018.interceptor.MyInterCeptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
</beans>
启动类加载配置文件
com.example.mybatis2018.Mybatis2018Application
package com.example.mybatis2018;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@MapperScan("com.example.mybatis2018.mapper")
@ImportResource(locations="classpath: interceptor.xml")//扫描xml文件,注入容器
@SpringBootApplication
public class Mybatis2018Application {
public static void main(String[] args) {
SpringApplication.run(Mybatis2018Application.class, args);
}
}