Spring注解@After,@Around,@Before的执行顺序?( 二 )


2018-11-23 16:31:59.796 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect - 请求参数:[]
2018-11-23 16:31:59.796 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect - ------------------------------------------------------
方法执行-----------
2018-11-23 16:31:59.806 [http-nio-8888-exec-9] DEBUG c.l.b.m.A.selectPrimaryKey - ==> Preparing: select * from article_detail where id = ?
2018-11-23 16:31:59.806 [http-nio-8888-exec-9] DEBUG c.l.b.m.A.selectPrimaryKey - ==> Preparing: select * from article_detail where id = ?
2018-11-23 16:31:59.806 [http-nio-8888-exec-9] DEBUG c.l.b.m.A.selectPrimaryKey - ==> Parameters: 1(Long)
2018-11-23 16:31:59.806 [http-nio-8888-exec-9] DEBUG c.l.b.m.A.selectPrimaryKey - ==> Parameters: 1(Long)
2018-11-23 16:31:59.814 [http-nio-8888-exec-9] DEBUG c.l.b.m.A.selectPrimaryKey - <== Total: 1
2018-11-23 16:31:59.814 [http-nio-8888-exec-9] DEBUG c.l.b.m.A.selectPrimaryKey - <== Total: 1
方法之后执行...after.
方法执行完执行...afterRunning
2018-11-23 16:31:59.824 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect - 耗时(毫秒):27
2018-11-23 16:31:59.824 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect - 返回数据:com.lmx.blog.common.Response@8675ce5
2018-11-23 16:31:59.824 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect - ==========================================>
可以看到,因为没有匹配@Around的规则,所以没有进行环绕通知 。(PS:我定义的环绕通知意思是要符合是 controller 包下的方法并且方法必须带有参数,而上述方法没有参数,所以只走了@before@after方法,不符合@Around的匹配逻辑)
我们再试一下另一个带有参数的方法
@RedisCache(type = Response.class)
@RequestMapping("/sendEmail")
public Response sendEmailToAuthor(String content){
System.out.println("测试执行次数");
return Response.ok(true);

以下是该部分代码的console打印
name:第二封邮件呢
方法环绕start...around
方法执行前执行......before
2018-11-23 16:34:55.347 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - <=====================================================
2018-11-23 16:34:55.347 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - 请求来源: =》0:0:0:0:0:0:0:1
2018-11-23 16:34:55.347 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - 请求URL:http://localhost:8888/user/sendEmail
2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - 请求方式:GET
2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - 响应方法:com.lmx.blog.controller.UserController.sendEmailToAuthor
2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - 请求参数:[第二封邮件呢]
2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - ------------------------------------------------------
测试执行次数
com.lmx.blog.common.Response@6d17f2fdaop String
方法环绕end...around
方法执行前执行......before
2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - <=====================================================
2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - 请求来源: =》0:0:0:0:0:0:0:1
2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - 请求URL:http://localhost:8888/user/sendEmail
2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - 请求方式:GET
2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - 响应方法:com.lmx.blog.controller.UserController.sendEmailToAuthor
2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - 请求参数:[第二封邮件呢]
2018-11-23 16:34:55.350 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - ------------------------------------------------------
测试执行次数
方法之后执行...after.
方法执行完执行...afterRunning
2018-11-23 16:34:55.350 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - 耗时(毫秒):0
2018-11-23 16:34:55.350 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - 返回数据:com.lmx.blog.common.Response@79f85428
2018-11-23 16:34:55.350 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - ==========================================>

显而易见,该方法符合@Around环绕通知的匹配规则,所以进入了@Around的逻辑,但是发现了问题,所有的方法都被执行了2次,不管是切面层还是方法层 。(有人估计要问我不是用的自定义注解


推荐阅读