关于通知的执行顺序问题
来源:4-2 入门案例
QQ71
2021-03-28 10:44:58
老师您好:
问题描述:
自己实验的执行顺序和网上说的不同,不知道哪里出了问题
相关截图:
相关代码:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="test1" class="com.imooc.aop.Test1"/>
<bean id="aspect1" class="com.imooc.aop.Aspect1"/>
<aop:aspectj-autoproxy/>
</beans>
相关代码:
package com.imooc.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test1 {
public void test1() throws InterruptedException {
System.out.println("test1");
}
public static void main(String[] args) throws InterruptedException {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Test1 t1 = applicationContext.getBean("test1",Test1.class);
t1.test1();
}
}
相关代码:
package com.imooc.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
@Aspect
public class Aspect1 {
@Pointcut("execution(* com.imooc.aop.Test1.*(..))")
public void pointcut1(){}
@Before("pointcut1()")
public void before(JoinPoint joinPoint){
System.out.println("==before==");
}
@AfterReturning("pointcut1()")
public void afterReturning(JoinPoint joinPoint){
System.out.println("==afterReturning==");
}
@After("pointcut1()")
public void after(){
System.out.println("==after==");
}
@Around("pointcut1()")
public Object testTime(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("===around begin===");
Object obj = proceedingJoinPoint.proceed();
System.out.println("===around end===");
return obj;
}
@AfterThrowing(value = "pointcut1()",throwing = "e")
public void afterThrowing(JoinPoint joinPoint,Throwable e){
System.out.println("出现异常");
e.printStackTrace();
}
}
1回答
好帮手慕阿满
2021-03-28
同学你好,这边测试通知的执行顺序和截图中所示的顺序是一致的,如:
首先是环绕通知的前置通知 -->前置通知 -->执行方法--->环绕通知的后置通知-->后置通知-->返回后通知。
这边建议同学多运行几遍,查看结果是否一致。
祝学习愉快~
相似问题