springboot 拦截器的坑
如果在拦截器里用自动注入了,
例:
public class MyInterceptor implements HandlerInterceptor {
@Autowired
private TestService testService;
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) {
testService.test("afterCompletion");
}
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3){
testService.test("postHandle");
}
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
testService.test("preHandle");
return true;
}
}
就要以下面这种方法注入bean,不然注入无效
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Bean
public MyInterceptor getMyInterceptor(){
return new MyInterceptor();
}
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(getMyInterceptor()).addPathPatterns("/**")
}
}
还有一种利用上下文对像获取bean实例,不用自动注入的方式就不受限制:
例:
public class MyInterceptor implements HandlerInterceptor {
private TestService testService = null;
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) {
testService.test("afterCompletion");
}
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3){
testService.test("postHandle");
}
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
if (testService == null){
testService = SpringContextUtil.getBean(TestService.class);
}
testService.test("preHandle");
return true;
}
}
SpringContextUtil工具类有两种都行:
1.注意在启动类里改成这样的:
ApplicationContext applicationContext = SpringApplication.run(TestApplication.class, args);
SpringContextUtil.setApplicationContext(applicationContext);
public class SpringContextUtil {
private static ApplicationContext applicationContext = null;
/**
* 注意
* 这个方法在启动的时候掉用把上下文对像set进去
* 例:
* public static void main(String[] args) {
* ApplicationContext applicationContext = SpringApplication.run(TestApplication.class, args);
* SpringContextUtil.setApplicationContext(applicationContext);
* }
* @param applicationContext
*/
public static void setApplicationContext(ApplicationContext applicationContext){
if(SpringContextUtil.applicationContext == null){
SpringContextUtil.applicationContext = applicationContext;
}
}
/**
* 获取applicationContext
* @return
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 通过name获取 Bean.
* @param name
* @return
*/
public static Object getBean(String name){
return getApplicationContext().getBean(name);
}
/**
* 通过class获取Bean.
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(Class<T> clazz){
return getApplicationContext().getBean(clazz);
}
/**
* 通过name,以及Clazz返回指定的Bean
* @param name
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(String name,Class<T> clazz){
return getApplicationContext().getBean(name, clazz);
}
}
2.实现ApplicationContextAware
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;
/**
* 注意
* 这个方法在启动的时候掉用把上下文对像set进去
* 例:
* public static void main(String[] args) {
* ApplicationContext applicationContext = SpringApplication.run(TestApplication.class, args);
* SpringContextUtil.setApplicationContext(applicationContext);
* }
* @param applicationContext
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext){
if(SpringContextUtil.applicationContext == null){
SpringContextUtil.applicationContext = applicationContext;
}
}
/**
* 获取applicationContext
* @return
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 通过name获取 Bean.
* @param name
* @return
*/
public static Object getBean(String name){
return getApplicationContext().getBean(name);
}
/**
* 通过class获取Bean.
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(Class<T> clazz){
return getApplicationContext().getBean(clazz);
}
/**
* 通过name,以及Clazz返回指定的Bean
* @param name
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(String name,Class<T> clazz){
return getApplicationContext().getBean(name, clazz);
}
}