记录:396
场景:实现自定义注解WriteLog,作用在类的方法上,每次执行方法就记录一条日志。使用AspectJ的注解@Aspect、@Pointcut、@Around、@Before、@AfterReturning、@AfterThrowing、@After拦截自定义注解WriteLog,从而完成记录日志功能。
AspectJ: The AspectJ weaver applies aspects to Java classes. It can be used as a Java agent in order to apply load-time weaving (LTW) during class-loading and also contains the AspectJ runtime classes.Eclipse AspectJ™ is a seamless aspect-oriented extension to the Java™ programming language.
版本:JDK 1.8,SpringBoot 2.6.3,aspectj-1.9.19
1.基础
1.1AspectJ依赖包
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.19</version>
</dependency>
1.2AspectJ的注解执行顺序
AspectJ的注解在生效时,注解执行顺序。
(1)没有抛出异常时,执行顺序:依次@Aspect、@Pointcut、@Around、@Before、@AfterReturning、@After、@Around。
(2)抛出异常时,执行顺序:依次@Aspect、@Pointcut、@Around、@Before、@AfterThrowing、@After、@Around。
注意:@Around出现两次,是因为@Bef

文章介绍了如何利用AspectJ的注解在SpringBoot环境中创建一个名为WriteLog的自定义注解,该注解用于方法上,每次执行方法时记录日志。通过@Aspect、@Pointcut、@Around等注解定义切点和通知,实现了在方法执行前后记录日志的功能,包括正常执行和异常处理的情况。同时,文章展示了如何在Controller中使用自定义注解,并提供了日志管理器的示例代码,用于实际的日志存储操作。
最低0.47元/天 解锁文章
5273

被折叠的 条评论
为什么被折叠?



