AOP核心概念

解析:

运用动态代理技术实现,创建了DeptServiceProxy代理对象,从而实现AOP的应用

AOP中的通知类型:

java 复制代码
package com.itheima.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@Aspect
public class MyAspect1 {
    //前置通知 - 目标方法运行之前运行
    @Before("execution(* com.itheima.service.impl.*.*(..))")
    public void before(){
        log.info("before....");
    }

    //环绕通知 - 目标方法运行之前和之后运行
    @Around("execution(* com.itheima.service.impl.*.*(..))")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        log.info("around ... before ....");
        Object result = pjp.proceed();
        log.info("around ... after ....");
        return result;
    }

    //后置通知 - 目标方法运行之后运行,无论是否出现异常都会执行
    @After("execution(* com.itheima.service.impl.*.*(..))")
    public void after(){
        log.info("after....");
    }

    //返回后通知 - 目标方法运行之后运行,如果出现异常不会运行
    @AfterReturning("execution(* com.itheima.service.impl.*.*(..))")
    public void afterReturning(){
        log.info("afterReturning....");
    }


    //返回后通知 - 目标方法运行之后运行,只有出现异常才会运行
    @AfterThrowing("execution(* com.itheima.service.impl.*.*(..))")
    public void afterThrowing(){
        log.info("afterThrowing....");
    }

}
java 复制代码
package com.itheima.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@Aspect
public class MyAspect1 {

    @Pointcut("execution(* com.itheima.service.impl.*.*(..))")
    public void pt(){

    }
    //前置通知 - 目标方法运行之前运行
    @Before("pt()")
    public void before(){
        log.info("before....");
    }

    //环绕通知 - 目标方法运行之前和之后运行
    @Around("pt()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        log.info("around ... before ....");
        Object result = pjp.proceed();
        log.info("around ... after ....");
        return result;
    }

    //后置通知 - 目标方法运行之后运行,无论是否出现异常都会执行
    @After("pt()")
    public void after(){
        log.info("after....");
    }

    //返回后通知 - 目标方法运行之后运行,如果出现异常不会运行
    @AfterReturning("pt()")
    public void afterReturning(){
        log.info("afterReturning....");
    }


    //返回后通知 - 目标方法运行之后运行,只有出现异常才会运行
    @AfterThrowing("pt()")
    public void afterThrowing(){
        log.info("afterThrowing....");
    }

}

通知的顺序:

可以通过@Order关键字来确定各个切面类前置通知或后置通知的顺序

execution切面表达式:

annotation切入点表达式(利用注解):

Service.impl中:

java 复制代码
@LogOperation
    @Override
    public List<Dept> list() {
        List<Dept> deptList = deptMapper.list();
        return deptList;
    }

一个LogOperation注解类:

java 复制代码
package com.itheima.anno;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)  //在方法中注解
@Retention(RetentionPolicy.RUNTIME)  //什么时候生效,运行的时候生效
public @interface LogOperation {

}

切面类:

java 复制代码
    @Before("@annotation(com.itheima.anno.LogOperation)")
    public void before(){
        log.info("MyAspect4 -> before ...");
    }

连接点:

相关推荐
卓怡学长8 分钟前
m280本科生导师指导平台
java·数据库·spring·tomcat·maven·intellij-idea
大尚来也9 分钟前
Serverless架构深度解析:适用场景、核心局限与破局之道
数据库
毛骗导演12 分钟前
@tencent-weixin/openclaw-weixin 插件深度解析(四):API 协议与数据流设计
前端·架构
毛骗导演16 分钟前
@tencent-weixin/openclaw-weixin 插件深度解析(二):消息处理系统架构
前端·架构
Wave84518 分钟前
非阻塞按键(单击,双击,长按)
数据库
2401_8318249624 分钟前
为你的Python脚本添加图形界面(GUI)
jvm·数据库·python
久违的太阳25 分钟前
记录一次ORACLE RAC安装PSU补丁步骤
数据库·oracle
2401_8796938726 分钟前
用Pygame开发你的第一个小游戏
jvm·数据库·python
一直都在57229 分钟前
Java死锁
java·开发语言
IT_陈寒34 分钟前
深入理解JavaScript:核心原理与最佳实践
前端·人工智能·后端