Spring AOP @After,@Around,@Before执行的顺序以及可能遇到的问题

您所在的位置:网站首页 around与round区别 Spring AOP @After,@Around,@Before执行的顺序以及可能遇到的问题

Spring AOP @After,@Around,@Before执行的顺序以及可能遇到的问题

#Spring AOP @After,@Around,@Before执行的顺序以及可能遇到的问题| 来源: 网络整理| 查看: 265

AOP中有@Before,@After,@Around,@AfterRunning注解等等。

首先上下自己的代码,定义了切点的定义

@Aspect @Component public class LogApsect { private static final Logger logger = LoggerFactory.getLogger(LogApsect.class); ThreadLocal startTime = new ThreadLocal(); // 第一个*代表返回类型不限 // 第二个*代表所有类 // 第三个*代表所有方法 // (..) 代表参数不限 @Pointcut("execution(public * com.lmx.blog.controller.*.*(..))") @Order(2) public void pointCut(){}; @Pointcut("@annotation(com.lmx.blog.annotation.RedisCache)") @Order(1) // Order 代表优先级,数字越小优先级越高 public void annoationPoint(){}; @Before(value = "annoationPoint() || pointCut()") public void before(JoinPoint joinPoint){ System.out.println("方法执行前执行......before"); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); logger.info(""); } @AfterThrowing("within(com.lmx.blog.controller.*Controller)") public void afterThrowing(){ System.out.println("异常出现之后...afterThrowing"); } }

@Before,@After,@Around注解的区别大家可以自行百度下。总之就是@Around可以实现@Before和@After的功能,并且只需要在一个方法中就可以实现。

首先我们来测试一个方法用于获取数据库一条记录的

@RequestMapping("/achieve") public Response achieve(){ System.out.println("方法执行-----------"); return Response.ok(articleDetailSercice.getPrimaryKeyById(1L)); }

以下是控制台打印的日志

方法执行前执行......before 2018-11-23 16:31:59.795 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect - 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 -


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3