7.Spring切入点的表达式和通知类型

AOP切入点与通知详解

1.切入点的表达式

表达式格式:

execution([修饰符] 返回值类型 包名.类名.方法名(参数))

其他的代替:

<!-- 完全指定一个方法 -->
<!-- <aop:before method="log" pointcut="execution(public void com.spring.demo1.UserServiceImpl.save())"/> -->        

<!-- 修饰符可以不写,不是必要出现的 -->
<!-- <aop:before method="log" pointcut="execution(void com.spring.demo1.UserServiceImpl.save())"/> -->

<!-- 返回值类型必须写,可以用【*】代替 -->
<!-- <aop:before method="log" pointcut="execution(* com.spring.demo1.UserServiceImpl.save())"/> -->

<!-- 包名必须写,可以用【*】代替 -->
<!-- <aop:before method="log" pointcut="execution(* *.spring.demo1.UserServiceImpl.save())"/> -->

<!-- 任意包结构,【*..*】 -->
<!-- <aop:before method="log" pointcut="execution(* *..*.UserServiceImpl.save())"/> -->

<!-- 类必须写,可以用【*】代替 -->
<!-- <aop:before method="log" pointcut="execution(* *..*.*ServiceImpl.save())"/> -->

<!-- 方法必须写,可以用【*】代替 -->
<!-- <aop:before method="log" pointcut="execution(* *..*.*ServiceImpl.save*())"/> -->

<!-- 参数必须写,【*】代表一个参数,【..】代表任意参数 -->
<!-- <aop:before method="log" pointcut="execution(* *..*.*ServiceImpl.save*(..))"/> -->

2.AOP通知类型

  1. 前置通知
        * 在目标类的方法执行之前执行。
        * 配置文件信息:

<aop:after method="before" pointcut-ref="myPointcut3"/>

        * 应用:可以对方法的参数来做校验
    2. 最终通知
        * 在目标类的方法执行之后执行,如果程序出现了异常,最终通知也会执行。
        * 在配置文件中编写具体的配置:

<aop:after method="after" pointcut-ref="myPointcut3"/>

        * 应用:例如像释放资源
    3. 后置通知
        * 方法正常执行后的通知。        
        * 在配置文件中编写具体的配置:

<aop:after-returning method="afterReturning" pointcut-ref="myPointcut2"/>

        * 应用:可以修改方法的返回值
    4. 异常抛出通知
        * 在抛出异常后通知
        * 在配置文件中编写具体的配置:

<aop:after-throwing method="afterThorwing" pointcut-ref="myPointcut3"/>

        * 应用:包装异常的信息
    5. 环绕通知
        * 方法的执行前后执行。
        * 在配置文件中编写具体的配置:

<aop:around method="around" pointcut-ref="myPointcut2"/>

        * 要注意:目标的方法默认不执行,需要使用ProceedingJoinPoint对来让目标对象的方法执行。

    public void around(ProceedingJoinPoint joinPoint){
        System.out.println("环绕通知1...");
        try {
            // 手动让目标对象的方法去执行
            joinPoint.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        System.out.println("环绕通知2...");
    }

如何将通知中的属性传入到切入点?????

如何将切入点中的属性传入到通知?????

转载于:https://www.cnblogs.com/NEWHOM/p/6803307.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值