zoukankan      html  css  js  c++  java
  • SpringAOP 通知(advice)

     
    
    @Aspect
    @Order(1)
    public class AopOne {
    
        /**
         * 目标方法执行之前
         * @param joinPoint
         */
        @Before("execution(* com.gary.operation.*.*(..))")
        public void befor(JoinPoint joinPoint) {
         MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();   System.out.println(
    "AopOne.befor()"); } /** * 目标方法执行以后 无论是否出现异常 相当于finally语句块 * 该方法优先于@AfterReturning * @param joinPoint */ @After("execution(* com.gary.operation.*.*(..))") public void after(JoinPoint joinPoint) { System.out.println("AopOne.after()"); } /** * 方法返回以后 没有出现异常的情况 */ @AfterReturning(pointcut="execution(* com.gary.operation.*.*(..))", returning="returnVal") public void afterReturning(JoinPoint joinPoint,Student returnVal) { System.out.println("AopOne.afterReturning()" + returnVal.getName()); } /** * 方法抛出异常时执行 * @param joinPoint */ @AfterThrowing(pointcut="execution(* com.gary.operation.*.*(..))", throwing="throwable") public void afterThrowing(JoinPoint joinPoint, Throwable throwable) { System.out.println("AopOne.afterThrowing()" + throwable.getMessage()); } /** * 环绕执行 可以自由控制目标方法 * @param proceedingJoinPoint * @return */ //@Around("execution(* com.gary.operation.*.*(..))") public Object around(ProceedingJoinPoint proceedingJoinPoint) { Object result = null; try { System.out.println("AopOne.around()-begin"); result = proceedingJoinPoint.proceed(); System.out.println("AopOne.around()-end"); } catch (Throwable e) { e.printStackTrace(); } return result; } }

    1、概念术语  

        在开始之前,需要理解Spring aop 的一些基本的概念术语(总结的个人理解,并非Spring官方定义):

      切面(aspect):用来切插业务方法的类。

      连接点(joinpoint):是切面类和业务类的连接点,其实就是封装了业务方法的一些基本属性,作为通知的参数来解析。

      通知(advice):在切面类中,声明对业务方法做额外处理的方法。

      切入点(pointcut):业务类中指定的方法,作为切面切入的点。其实就是指定某个方法作为切面切的地方。

      目标对象(target object):被代理对象。

      AOP代理(aop proxy):代理对象。

      通知:

      前置通知(before advice):在切入点之前执行。

      后置通知(after advice): 相当于finally语句块,无论是否出现异常都执行

      后置通知(after returning advice):在切入点执行完成后,执行通知。(出现异常不执行)

      环绕通知(around advice):包围切入点,调用方法前后完成自定义行为。

      异常通知(after throwing advice):在切入点抛出异常后,执行通知。

    2、常用表达式总结

    1、任何一个目标对象声明的类型有一个 @Transactional 注解的连接点

    @Pointcut("@within(org.springframework.transaction.annotation.Transactional)")

    2、任何一个执行的方法有一个 @Transactional 注解的连接点

    @Pointcut("@annotation(org.springframework.transaction.annotation.Transactional)")

    3、任何一个只接受一个参数,并且运行时所传入的参数是java.lang.String接口的连接点

    @Pointcut("args(java.lang.String)")

    4、任何一个在名为'tradeService'的Spring bean之上的连接点

    @Pointcut("bean(UserDao)")

    任何一个在名字匹配通配符表达式'*Service'的Spring bean之上的连接点

    @Pointcut("bean(*Dao)")

    5、组合使用 && 并且的意思。 即俩个条件都需要满足。

    @Before("aPointcut() && args(java.lang.String)")

    6、组合使用并且接收参数

        @Before("aPointcut() && args(s)")
        public void beforeAdvice(String s) {
            System.out.println("before advice is executed!" + s);
        }

    7、组合使用接收参数和注解

    1     @Before("aPointcut() && args(s) && @annotation(tx)")
    2     public void beforeAdvice(String s, Transactional tx) {
    3         System.out.println("before advice is executed!" + tx);
    4     }

    8、组合使用接收注解

    1    @Before(value="execution(* com.gary.operation.demo.proxy.*.*(..)) && @annotation(tx)")
    2     public void beforeAdvice(Transactional tx) {
    3         System.out.println("before advice is executed!" + tx);
    4     }
  • 相关阅读:
    Go 笔记之如何防止 goroutine 泄露
    调试 Go 的代码生成
    使用k8s容器钩子触发事件
    springboot教程
    Intellij IDEA 使用Spring-boot-devTools无效解决办法
    c# WMI获取机器硬件信息(硬盘,cpu,内存等)
    各式 Web 前端開發工具整理
    Informix 中执行多条SQL(Execute Script)
    Log4Net
    mysql 按年度、季度、月度、周、日SQL统计查询
  • 原文地址:https://www.cnblogs.com/daxin/p/3355050.html
Copyright © 2011-2022 走看看