一,filter/interceptor/aop生效的先后顺序?

1,filter即过滤器,基于servlet容器,处于最外层,

所以它会最先起作用,最后才停止

说明:filter对所有访问到servlet容器的url都有效,包括静态资源

2,interceptor即拦截器,基于web框架,它会在filter之后起作用

说明:spring boot 1.x中,静态资源已被interceptor排除,

spring boot 2.x中,需要自己手动排除到静态资源的访问

filter和interceptor都是作用于请求

3,aop即切面,基于Spring的IOC容器,对spring管理的bean有效,

它会在interceptor之后才生效

aop可以作用于类和方法

如图:

说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

对应的源码可以访问这里获取: https://github.com/liuhongdi/

说明:作者:刘宏缔 邮箱: 371125307@qq.com

二,演示项目的相关信息

1,项目地址:

https://github.com/liuhongdi/costtime

2,项目的原理:

项目中使用了两个filter,两个interceptor,两个aspect,

功能分别都是:计算请求或方法执行的时间,   打印请求的参数

然后观察它们被执行到的顺序

3,项目结构:如图:

三,配置文件说明 :

pom.xml

        <!--aop begin-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

用来引入aop

四,java代码说明

1,DefaultMvcConfig.java

@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class DefaultMvcConfig implements WebMvcConfigurer {
@Resource
private LogInterceptor logInterceptor;
@Resource
private CostTimeInterceptor costTimeInterceptor; @Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("/home/home");
} //添加Interceptor
@Override
public void addInterceptors(InterceptorRegistry registry) {
//1.加入的顺序就是拦截器执行的顺序,
//2.按顺序执行所有拦截器的preHandle
//3.所有的preHandle 执行完再执行全部postHandle 最后是postHandle
registry.addInterceptor(costTimeInterceptor)
.addPathPatterns("/home/home**")
.excludePathPatterns("/html/*","/js/*");
registry.addInterceptor(logInterceptor)
.addPathPatterns("/**")
.excludePathPatterns("/html/*","/static/**","/images/**");
} //add filter
@Bean
public FilterRegistrationBean addTimeFilterBean() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new TimeFilter());
registration.setName("timeFilter");
registration.setOrder(2); //请求中过滤器执行的先后顺序,值越小越先执行
registration.addUrlPatterns("/home/*","/abc/*");
return registration;
} @Bean
public FilterRegistrationBean addLogFilterBean() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new LogFilter());
registration.setName("logFilter");
registration.setOrder(1); //请求中过滤器执行的先后顺序,值越小越先执行
registration.addUrlPatterns("/*");
registration.addInitParameter("exclusions","/js/*,/images/*,*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid2/*");
return registration;
}
}

说明:拦截器是按加入到registry的顺序执行

filter是按setOrder中指定的顺序执行

另外:filter也可以用Order注解来指定顺序

2,CostTimeAspect.java

@Component
@Aspect
@Order(3)
public class CostTimeAspect {
ThreadLocal<Long> startTime = new ThreadLocal<>();
@Pointcut("execution(public * com.costtime.demo.controller.*.*(..))")
private void pointcut() {} //用around得到方法使用的时间
@Around(value = "pointcut()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("------costtime aop around begin");
long begin = System.nanoTime();
Object obj=joinPoint.proceed();
long end =System.nanoTime();
long timeMicro = (end-begin)/1000;
System.out.println("costtime aop 方法around:微秒数:"+timeMicro);
System.out.println("------costtime aop around end");
return obj;
} @Before("pointcut()")
public void doBefore(JoinPoint joinPoint) throws Throwable{
System.out.println("------costtime aop doBefore begin");
startTime.set(System.currentTimeMillis());
} //和doBefore搭配,得到使用的时间
@AfterReturning(returning = "ret" , pointcut = "pointcut()")
public void doAfterReturning(Object ret){
System.out.println("------costtime aop doAfterReturning begin");
System.out.println("costtime aop 方法doafterreturning:毫秒数:"+ (System.currentTimeMillis() - startTime.get()));
}
}

3,LogAspect.java

@Component
@Aspect
@Order(1)
public class LogAspect {
@Pointcut("execution(public * com.costtime.demo.controller.*.*(..))")
private void pointcut() {} @Around(value = "pointcut()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("------log aop around begin");
Object obj=joinPoint.proceed();
System.out.println("------log aop around end");
return obj;
} //把请求参数打印出来
@Before("pointcut()")
public void doBefore(JoinPoint joinPoint) throws Throwable{
System.out.println("------log aop doBefore begin");
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest(); //得到方法的参数名和参数值
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature)signature;
String[] paramNames = methodSignature.getParameterNames();
Object[] args = joinPoint.getArgs();
String paramValue = "";
Map<String, Object> nameAndArgs = new HashMap<String, Object>();
for (int i = 0; i < paramNames.length; i++) {
paramValue+="parameter:"+paramNames[i]+";value:"+args[i];
} // 记录下请求内容
System.out.println("log aop URL : " + request.getRequestURL().toString());
System.out.println("log aop PARAM : " + request.getQueryString());
System.out.println("log aop HTTP_METHOD : " + request.getMethod());
System.out.println("log aop IP : " + request.getRemoteAddr());
System.out.println("log aop METHOD CLASS : " + joinPoint.getSignature().getDeclaringTypeName() );
System.out.println("log aop METHOD NAME: " + joinPoint.getSignature().getName());
System.out.println("log aop METHOD ARGS : " + paramValue);
} @AfterReturning(returning = "ret" , pointcut = "pointcut()")
public void doAfterReturning(Object ret){
System.out.println("------log aop doAfterReturning begin");
}
}

说明:这两个aspect用Order注解来指定执行的先后顺序,

值越小执行顺序越靠前

4,过滤器和拦截器的代码因为功能基本一致,为节省篇幅,不再贴出,

大家可以从github上访问:

https://github.com/liuhongdi/costtime

五,测试启动顺序的效果

1,访问url:

http://127.0.0.1:8080/home/home?v=1

查看控制台的输出:

----------------log filter doFilter begin
===执行过滤器功能
log filter URL : http://127.0.0.1:8080/home/home
log filter PARAM : v=1
log filter HTTP_METHOD : GET
log filter IP : 127.0.0.1
----------------time filter doFilter begin---------------time interceptor preHandle
[interceptor] request parameters: name:v;value:1
---------------log interceptor preHandle
[interceptor] request parameters: name:v;value:1------log aop around begin
------log aop doBefore begin
log aop URL : http://127.0.0.1:8080/home/home
log aop PARAM : v=1
log aop HTTP_METHOD : GET
log aop IP : 127.0.0.1
log aop METHOD CLASS : com.costtime.demo.controller.HomeController
log aop METHOD NAME: homeMethod
log aop METHOD ARGS : parameter:version;value:1
------costtime aop around begin
------costtime aop doBefore begin
------costtime aop doAfterReturning begin
costtime aop 方法doafterreturning:毫秒数:1027
costtime aop 方法around:微秒数:1027548
------costtime aop around end
------log aop doAfterReturning begin
------log aop around end
---------------log interceptor postHandle
---------------time interceptor postHandle
time interceptor 方法 postHandle:毫秒数:1108
---------------log interceptor afterCompletion
---------------time interceptor afterCompletion
timefilter: /home/home costtime: 1128ms
----------------time filter doFilter end
----------------log filter doFilter end

2,可以看到:

大类的启动顺序是:

filter

interceptor

aop

3,可以看到

filter的启动顺序,是按我们在config中设定的order顺序

interceptor的启动顺序,是addInterceptor到registry的顺序

同一个interceptor内部:执行顺序是: preHandle,postHandle,afterCompletion

aop的启动顺序:是我们在Order注解中指定的顺序

同一个aop内部:around比dobefore启动更早

六,查看spring boot的版本

  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.1.RELEASE)

spring boot:多个filter/多个interceptor/多个aop时设置调用的先后顺序(spring boot 2.3.1)的更多相关文章

  1. 非spring组件servlet、filter、interceptor中注入spring bean

    问题:在filter和interceptor中经常需要调用Spring的bean,filter也是配置在web.xml中的,请问一下这样调用的话,filter中调用Spring的某个bean,这个be ...

  2. [转] Spring Boot实战之Filter实现使用JWT进行接口认证

    [From] http://blog.csdn.net/sun_t89/article/details/51923017 Spring Boot实战之Filter实现使用JWT进行接口认证 jwt(j ...

  3. 曹工说Spring Boot源码(16)-- Spring从xml文件里到底得到了什么(aop:config完整解析【上】)

    写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...

  4. spring cloud gateway之filter篇

    转载请标明出处: https://www.fangzhipeng.com 本文出自方志朋的博客 在上一篇文章详细的介绍了Gateway的Predict,Predict决定了请求由哪一个路由处理,在路由 ...

  5. Spring MVC中各个filter的用法

    转载:http://blog.csdn.net/qyp1314/article/details/42023725 Spring MVC中各个filter的用法 2014-12-19 09:08 105 ...

  6. Spring Cloud Zuul网关 Filter、熔断、重试、高可用的使用方式。

    时间过的很快,写springcloud(十):服务网关zuul初级篇还在半年前,现在已经是2018年了,我们继续探讨Zuul更高级的使用方式. 上篇文章主要介绍了Zuul网关使用模式,以及自动转发机制 ...

  7. spring boot继承web和mybatis时,调用接口删除记录出现的空指针以及解决办法

    前两天在学spring boot的时候,出现了一个很奇怪的错误,因为是第一次使用spring boot,所以没想到会遇到这种莫名其妙的bug,即调用接口删除数据库中一条记录的时候,数据库中记录事实上以 ...

  8. JAVA Spring MVC中各个filter的用法

    spring mvc的org.springframework.web.filter包下的Java文件如下: 类的结构如下: AbstractRequestLoggingFilter及其子类 Abstr ...

  9. Filter(过滤器)与Interceptor(拦截器)的区别

    Filter能够对请求和响应资源进行拦截: Interceptor只针对请求进行拦截 在 Struts2中: (1)拦截器是基于java反射机制的,而过滤器是基于函数回调的. (2)过滤器依赖与ser ...

随机推荐

  1. jenkins 下载插件失败 有效的处理办法(亲测)

    jenkins 下载插件失败,提示: java.io.IOException: Downloaded file /app/jenkins_home/plugins/jacoco.jpi.tmp doe ...

  2. 你还在寻找Navicat的破解版本?你应该了解开源免费的DBeaver

    前言 你是否还在各个"免费绿色"的下载网站上寻找navicat的破解版本,或者已经通过某些方式破解了navicat的特定版本.你或者是在一家对安全和软件著作权比较看重的公司,明令禁 ...

  3. Hashmap,Set,Map,List,ArrayList的区别

    表格: 类型 默认容量 加载因子[1] 扩容增量 底层实现 是否安全及同步方式 Vector 10 1 2倍 Object数组 安全,synchronized ArrayList 10 1 1.5倍( ...

  4. 分布式服务(RPC)+分布式消息队列(MQ)面试题精选

    ​ 分布式系统(distributed system)是建立在网络之上的软件系统.正是因为软件的特性,所以分布式系统具有高度的内聚性和透明性.因此,网络和分布式系统之间的区别更多的在于高层软件(特别是 ...

  5. 针对于Java的35 个代码性能优化总结

    针对于Java的35 个代码性能优化总结前言代码优化,一个很重要的课题.可能有些人觉得没用,一些细小的地方有什么好修改的,改与不改对于代码的运行效率有什么影响呢?这个问题我是这么考虑的,就像大海里面的 ...

  6. 加权图的最小生成树、最短路径算法 - java实现

    加权图相关算法 前言 本文主要介绍加权图算法中两个重要应用:最小生成树和最短路径. 求最小生成树时针对的是加权无向图,加权有向图的最小生成树算法成为"最小属树形图"问题,较为复杂, ...

  7. Pycharm默认输入状态是insert状态,选中文字无法直接输入替换或删除

    最近在学习Python,使用pycharm的时候,我的光标处于加粗状态,也就是编程软件经常出现的insert插入编辑模式,我就点击了一下insert按键,退出了这个模式,但是我每次打开都是会处于这种模 ...

  8. 高德AR & 车道级导航技术演进与实践

    2020云栖大会于9月17日-18日在线上举行,阿里巴巴高德地图携手合作伙伴精心组织了"智慧出行"专场,为大家分享高德地图在打造基于DT+AI和全面上云架构下的新一代出行生活服务平 ...

  9. Android小部件Widget开发过程中的坑和总结

    @ 目录 概述 官方参考 效果图 AndroidManifest.xml Receiver Service Options res/xml/ widget_desktop_options.xml 常用 ...

  10. Python-进程-进程池-原理

    进程 资源集合,调度和分配资源,说到进程就不得不提到线程,线程和进程是密不可分,进程申请了资源,但真正使用资源的是线程,其实本质上类似面向对象的思想,面向对象把数据和数据的操作封装在一个类中,进程把资 ...