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 ...");
    }

连接点:

相关推荐
scott.cgi8 小时前
Unity直接编译Java文件作为插件,导致失败的两个打包设置
java·unity·unity调用java·unity的java文件·unity的android插件·unity调用android·unity加载java代码
KaMeidebaby11 小时前
卡梅德生物技术快报|骆驼纳米抗体:从原核表达、高通量测序到分子对接全流程实现
前端·数据库·其他·百度·新浪微博
澈20712 小时前
C++并查集:高效解决连通性问题
java·c++·算法
子兮曰13 小时前
Node.js v26.1.0 深度解读:FFI、后量子密码与调试器的进化
前端·后端·node.js
测试员周周13 小时前
【Appium 系列】第06节-页面对象实现 — LoginPage 实战
开发语言·前端·人工智能·python·功能测试·appium·测试用例
2401_8734794014 小时前
运营活动被薅羊毛怎么防?用IP查询+设备指纹联动封堵漏洞
java·网络·tcp/ip·github
ShiJiuD66688899914 小时前
大事件板块一
java
摇滚侠14 小时前
@Autowired 和 @Resource 的区别
java·开发语言
2301_7838486514 小时前
优化文本分类中堆叠模型的网格搜索性能:避免训练卡顿的实战指南
jvm·数据库·python
SeaTunnel14 小时前
(八)收官篇 | 数据平台最后一公里:数据集成开发设计与上线治理实战
java·大数据·开发语言·白鲸开源