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

连接点:

相关推荐
刘发财4 小时前
弃用html2pdf.js,这个html转pdf方案能力是它的几十倍
前端·javascript·github
牛奶6 小时前
2026年大模型怎么选?前端人实用对比
前端·人工智能·ai编程
牛奶6 小时前
前端人为什么要学AI?
前端·人工智能·ai编程
皮皮林5517 小时前
Java性能调优黑科技!1行代码实现毫秒级耗时追踪,效率飙升300%!
java
冰_河7 小时前
QPS从300到3100:我靠一行代码让接口性能暴涨10倍,系统性能原地起飞!!
java·后端·性能优化
Kagol9 小时前
🎉OpenTiny NEXT-SDK 重磅发布:四步把你的前端应用变成智能应用!
前端·开源·agent
GIS之路10 小时前
ArcGIS Pro 中的 notebook 初识
前端
JavaGuide10 小时前
7 道 RAG 基础概念知识点/面试题总结
前端·后端
桦说编程10 小时前
从 ForkJoinPool 的 Compensate 看并发框架的线程补偿思想
java·后端·源码阅读