HandlerInterceptorAdapter

spring 拦截器 

 

1.  

@Component
public class myInterceptor extends HandlerInterceptorAdapter

 

2.    重写此方法

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
myInterceptor annotation = method.getAnnotation(myInterceptor.class);
return null;
}
return super.preHandle(request, response, handler);

 

 

3. 注册此拦截器

@Configuration
public class MvcConfig implements WebMvcConfigurer

 

@Override
public void addInterceptors(InterceptorRegistry registry) {
log.info("注册拦截器");
registry.addInterceptor(myInterceptor).addPathPatterns(myInterceptor.split(","));
}

 

 

注册一个Bean

@Configuration
public class ActivitiIncludeConfig implements BeanPostProcessor {

@PostConstruct
public void run() {
String includeFlowVersion = ServiceFactory.getPropertiesValue("includeFlowVersion");

String version = ServiceFactory.getPropertiesValue("app.version");
// 如果没有配置这个属性 或者 这个属性配置为false
if(StringUtils.isBlank(includeFlowVersion) || !TypeUtils.castToBoolean(includeFlowVersion)){
return ;
}
Class<?> aClass = null;
try {
aClass = Class.forName("org.activiti.engine.impl.db.DbSqlSession");
Field versionField = aClass.getDeclaredField("ACTIVITI_VERSIONS");
ReflectionUtils.makeAccessible(versionField);
List<ActivitiVersion> lists = (List)versionField.get(null);
lists.add(new ActivitiVersion("6.4.1.3"));
Object o = versionField.get(null);
System.out.println(o);
} catch (Exception e) {
e.printStackTrace();
}

}


}
posted @ 2021-06-03 18:12  java_小跟班  阅读(209)  评论(0编辑  收藏  举报