springboot aop

/**
 * @Component 注解 把切面类加入到IOC容器中
 * @Aspect 注解 使之成为切面类
 */

@Aspect
@Component
public class WebLogAcpect02 {

    /**
     * 定义切入点,切入点为com.example.demo.controller.UserController下的testObject(..)函数
     */
    @Pointcut("execution(public * com.example.demo.controller.UserController.testObject(..))")
    public void webLog() {
    }

    /**
     * JoinPoint 当前需要增强的函数
     * 例:execution(User com.example.demo.controller.UserController.testObject(Integer,String,Integer,String))
     */

    /**
     * 前置通知,方法调用前被调用
     */
    @Before("webLog()")
    public void deBefore() {
        System.out.println("前置通知");
    }

    /**
     * 后置通知
     */
    @AfterReturning("webLog()")
    public void deAfterReturning() {
        System.out.println("后置通知");
    }

    /**
     * 环绕通知
     */
    @Around("webLog()")
    public Object deAround(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕前增强=============");
        Object obj = joinPoint.proceed();//当前增强的函数
        System.out.println("环绕后增强=============");
        return obj;
    }

    /**
     * 异常抛出通知
     */
    @AfterThrowing(value = "webLog()",throwing = "e")
    public void deAfterThrowing(Throwable e){
        System.out.println("异常抛出增强=========="+e.getMessage());
    }

    /**
     * 后置最终通知,不管是抛出异常或者正常退出都会执行
     */
    @After("webLog()")
    public void deAfter(){
        System.out.println("后置最终通知");
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值