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

连接点:

相关推荐
Leo July5 小时前
【Java】Spring Security 6.x 全解析:从基础认证到企业级权限架构
java·spring·架构
聆风吟º5 小时前
Oracle到KingbaseES数据库迁移:全流程实战指南与避坑总结
数据库·oracle·数据库迁移·kingbasees
哈__5 小时前
Oracle至KingbaseES数据库迁移全攻略:痛点拆解、配置实操与问题排查
数据库·oracle
星火开发设计6 小时前
C++ 数组:一维数组的定义、遍历与常见操作
java·开发语言·数据结构·c++·学习·数组·知识
码道功成6 小时前
Pycham及IntelliJ Idea常用插件
java·ide·intellij-idea
search76 小时前
前端设计:CRG 3--CDC error
前端
治金的blog6 小时前
vben-admin和vite,ant-design-vue的结合的联系
前端·vscode
JZC_xiaozhong6 小时前
电商ERP如何同步订单数据到MySQL?集成方案解析
数据库·mysql·数据分析·etl工程师·嵌入式实时数据库·电商erp集成·数据集成与应用集成
消失的旧时光-19436 小时前
第四篇(实战): 订单表索引设计实战:从慢 SQL 到毫秒级
java·数据库·sql
知识分享小能手6 小时前
Oracle 19c入门学习教程,从入门到精通, Oracle 表空间与数据文件管理详解(9)
数据库·学习·oracle