Spring笔记4-AOP,前置通知,后置通知,返回通知,异常通知

<beans>
    <!--配置自动扫描的包-->
    <context:componemt-scan base-package="com.kcj.test"></context:componemt-scan>

    <!--使用Aspectj注解,自动为匹配的类生成代理对象-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
package com.kcj.test;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Order(1)//设置切面的优先级,数字越小优先级越高
@Aspect//声明为一个切面
@Component//放到IOC容器
public class LoggingAspect {

    /***
     * 定义一个方法来声明切入点,提供引用
     */
    @Pointcut("execution(* com.kcj.test.*.*(..))")
    public void declareJoinPointExxcution(){
    }

   /***
     * 声明该方法是一个前置通知,方法执行之前执行
     */
    @Before("LoggingAspect.declareJoinPointExxcution()")
    public void before(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
    }

    /***
     * 声明该方法是一个后置通知,目标方法执行后执行,就算目标方法抛出异常也会执行
     * 但是后置通知不能访问目标方法返回的结果
     */
    @Before("declareJoinPointExxcution()")
    public void after(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
    }

    /***
     * 在目标方法正常执行完了之后执行,可以访问到方法的返回值:result
     */
    @AfterReturning(value = "declareJoinPointExxcution()", returning = "result")
    public void afterReturning(JoinPoint joinPoint, Object result) {
    }

    /***
     * 在目标方法抛出异常执行,也可以指定出现制定异常时执行,Exception改为需要指定的一场类型
     */
    @AfterThrowing(value = "execution(* com.kcj.test.*.*(..)))",throwing = "e")
    public void afterThorwing(JoinPoint joinPoint, Exception e) {
    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值