程序员社区

【Spring学习及总结19】为通知方法传递参数

  1. 通知方法执行顺序:
    正常执行:
    @Before(前置通知)–> @After(后置通知)–> @AfterReturning(正常返回)
    异常执行:
    @Before(前置通知)–> @After(后置通知)–> @AfterThrowing(方法异常)
try {
      @Before
      method.invoke(obj,args);
      @AfterReturning
   } catch (BeansException e) {
      @AfterThrowing
   } finally {
      @After
   }
  1. 获取通知方法的详细信息
@Aspect
@Component
public class LogUtils {
    //在目标方法执行之前执行
    //exception(访问权限符 返回值类型 方法签名)
    @Before("execution(public int com.hh.pojo.MyMath.*(int,int))")
    public static void logStart(JoinPoint joinPoint){
        //获取目标方法运行时使用的参数
        Object[] args = joinPoint.getArgs();
        //获取到方法签名
        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        System.out.println("【"+name+"】方法开始执行,用的参数列表【"+Arrays.asList(args)+"】");
    }

    //在目标方法执行之后执行
    @AfterReturning(value = "execution(public int com.hh.pojo.MyMath.*(int,int))",returning = "result")
    public static void logReturn(JoinPoint joinPoint,Object result){
        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        System.out.println("【"+name+"】方法正常执行完成,计算结果是:"+result);
    }

    //在目标方法出现异常时执行
    @AfterThrowing(value = "execution(public int com.hh.pojo.MyMath.*(int,int))",throwing = "exception")
    public static void logException(JoinPoint joinPoint,Exception exception){
        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        System.out.println("【"+name+"】方法执行出现异常了,异常信息是:"+exception+"这个异常通知");
    }

    //在目标方法结束的时候执行
    @After("execution(public int com.hh.pojo.MyMath.*(int,int))")
    public static void logEnd(JoinPoint joinPoint){
        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        System.out.println("【"+name+"】方法最终结束了");
    }
}
public class MyTest {
    ApplicationContext context =
            new ClassPathXmlApplicationContext("spring.xml");
    @Test
    public void test(){
        Caculator bean = bean = context.getBean(Caculator.class);;
        bean.add(1,2);
    }
}

在这里插入图片描述
3. 抽取切入点表达式:

@Aspect
@Component
public class LogUtils {

    @Pointcut("execution(public int com.hh.pojo.MyMath.*(int,int))")
    public void MyPoint(){

    };

    //在目标方法执行之前执行
    //exception(访问权限符 返回值类型 方法签名)
    @Before("MyPoint()")
    public static void logStart(JoinPoint joinPoint){
        //获取目标方法运行时使用的参数
        Object[] args = joinPoint.getArgs();
        //获取到方法签名
        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        System.out.println("【"+name+"】方法开始执行,用的参数列表【"+Arrays.asList(args)+"】");
    }

    //在目标方法执行之后执行
    @AfterReturning(value = "MyPoint()",returning = "result")
    public static void logReturn(JoinPoint joinPoint,Object result){
        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        System.out.println("【"+name+"】方法正常执行完成,计算结果是:"+result);
    }

    //在目标方法出现异常时执行
    @AfterThrowing(value = "MyPoint()",throwing = "exception")
    public static void logException(JoinPoint joinPoint,Exception exception){
        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        System.out.println("【"+name+"】方法执行出现异常了,异常信息是:"+exception+"这个异常通知");
    }

    //在目标方法结束的时候执行
    @After("MyPoint()")
    public static void logEnd(JoinPoint joinPoint){
        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        System.out.println("【"+name+"】方法最终结束了");
    }
}

赞(0) 打赏
未经允许不得转载:IDEA激活码 » 【Spring学习及总结19】为通知方法传递参数

相关推荐

  • 暂无文章

一个分享Java & Python知识的社区