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

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(){};

【Spring注解@After,@Around,@Before的执行顺序?】@Before(value = https://www.isolves.com/it/cxkf/bk/2023-04-04/"annoationPoint() || pointCut()")
public void before(JoinPoint joinPoint){
System.out.println("方法执行前执行......before");
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
logger.info("<=====================================================");
logger.info("请求来源: =》" + request.getRemoteAddr());
logger.info("请求URL:" + request.getRequestURL().toString());
logger.info("请求方式:" + request.getMethod());
logger.info("响应方法:" + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
logger.info("请求参数:" + Arrays.toString(joinPoint.getArgs()));
logger.info("------------------------------------------------------");
startTime.set(System.currentTimeMillis());
}

// 定义需要匹配的切点表达式,同时需要匹配参数
@Around("pointCut() && args(arg)")
public Response around(ProceedingJoinPoint pjp,String arg) throws Throwable{
System.out.println("name:" + arg);
System.out.println("方法环绕start...around");
String result = null;
try{
result = pjp.proceed().toString() + "aop String";
System.out.println(result);
}catch (Throwable e){
e.printStackTrace();
}
System.out.println("方法环绕end...around");
return (Response) pjp.proceed();
}

@After("within(com.lmx.blog.controller.*Controller)")
public void after(){
System.out.println("方法之后执行...after.");
}

@AfterReturning(pointcut="pointCut()",returning = "rst")
public void afterRunning(Response rst){
if(startTime.get() == null){
startTime.set(System.currentTimeMillis());
}
System.out.println("方法执行完执行...afterRunning");
logger.info("耗时(毫秒):" + (System.currentTimeMillis() - startTime.get()));
logger.info("返回数据:{}", rst);
logger.info("==========================================>");
}

@AfterThrowing("within(com.lmx.blog.controller.*Controller)")
public void afterThrowing(){
System.out.println("异常出现之后...afterThrowing");
}

}
@Before@After@Around注解的区别大家可以自行百度下 。
总之就是@Around可以实现@Before@After的功能,并且只需要在一个方法中就可以实现 。
首先我们来测试一个方法用于获取数据库一条记录的 。JAVA进阶路线:https://www.yoodb.com/
@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 - <=====================================================
2018-11-23 16:31:59.795 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect - 请求来源: =》0:0:0:0:0:0:0:1
2018-11-23 16:31:59.795 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect - 请求URL:http://localhost:8888/user/achieve
2018-11-23 16:31:59.795 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect - 请求方式:GET
2018-11-23 16:31:59.795 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect - 响应方法:com.lmx.blog.controller.UserController.achieve


推荐阅读