spring AOP基本概念及使用

1.基本概念

AOP(Aspect-Oriented Programming,面向切面编程),它提供了一种编程范式,用于将那些与业务逻辑无关的行为(如日志、权限、事务管理等)封装到可重用的模块中。即松耦合,将公共动作抽离、聚集起来,在不动业务代码的情况下,加入额外的公共逻辑。提前声明,本文描述的都是基于java动态代理的AOP。

1.1 Spring AOP的编程术语

  • 切面(Aspect):定义通知(Advice)和切点(PointCut)的地方。

  • 通知(Advice):又名"增强",定义了切面要完成的工作 以及何时执行 这个工作。即你要添加的公共逻辑

    • 官方原文:action to take at a joinpoint(作用在连接点上的动作)
  • 切点(PointCut):定义通知(Advice)作用于那些连接点

    • 官方原文:A pointcut is composed of a {@link ClassFilter} and a {@link MethodMatcher}(一个pointcut是由ClassFilter和MethodMatcher组成)
  • 连接点(Jointpoint):应用执行过程中能够插入切面的一个点,它可以是一个方法、构造函数、一个字段。即通知(Advice)要作用、影响的地方。在spring中仅支持method

    • 官方原文:a runtime joinpoint is then the reification of an access to an accessible object (a method, a constructor, a field)
  • 引入(Introduction):引入允许我们向现有的类添加新的方法或属性

  • 前4个概念比较重要,平时用到的多,用图表示他们的关系:

1.2 通知(advice)的类型

类型 释义
Around 环绕通知
Before 前置通知
AfterReturning 后置通知
AfterThrowing 异常通知
After 最终通知

用图表示"通知"的类型: ps:同时声明5个类型的Advice,他们的执行顺序也是这样的。

1.3 切点表达式

spring支持AspectJ的切点表达式,execution表达式语法。

sql 复制代码
execution(<modifiers-pattern?> <ret-type-pattern> <declaring-type-pattern?><name-pattern>(<param-pattern>) <throws-pattern?>)

execution(<修饰符?> <返回值> <类路径?> <方法名>(<参数类型>) <异常类型?>)

带?的可以省略

这个方面资料看的不多,可以自行度娘、AI,基础使用很简单。

2. 使用

声明接口

java 复制代码
public interface UserService {  
    String myName(String name);  
}

声明实现类

java 复制代码
@Service  
public class UserServiceImpl implements UserService {  
    @Override  
    public String myName(String name) {  
        System.out.println("i'm" + name);  
        return name;  
    }  
}

声明切面

java 复制代码
@Configuration  
@EnableAspectJAutoProxy  
@Aspect  
public class AspectDemo {  
    //环绕通知  
    @Around(value = "execution(* com.dy..*.*(..))")  
    public Object aopAround(ProceedingJoinPoint point) throws Throwable {  
        System.out.println("Around advice before method execute !!!");  
        try {  
            return point.proceed();  
        } finally {  
            System.out.println("Around advice after method execute !!!");  
        }  
    }  
  
    // 前置通知  
    @Before(value = "execution(* com.dy..*.*(..))")  
    public void aopBefore() {  
        System.out.println("before advice method execute !!!");  
    }  
  
    // 最终通知  
    @After(value = "execution(* com.dy..*.*(..))")  
    public void aopAfter() {  
        System.out.println("after advice method execute !!!");  
    }  
  
    // 后置通知  
    @AfterReturning(value = "execution(* com.dy..*.*(..))")  
    public void aopAfterReturn() {  
        System.out.println("afterReturn advice method execute !!!");  
    }  
  
    // 异常通知  
    @AfterThrowing(value = "execution(* com.dy..*.*(..))")  
    public void aopAfterThrowing() {  
        System.out.println("afterThrow advice method execute !!!");  
    }  
}

运行

java 复制代码
@SpringBootApplication  
public class App {  
    public static void main(String[] args) {  
        ConfigurableApplicationContext context = SpringApplication.run(App.class);  
        UserService userService =(UserService)context.getBeanFactory().getBean("userServiceImpl");  
        userService.myName("hh");  
    }  
}

运行结果

java 复制代码
Around advice before method execute !!!  // 环绕通知
before advice method execute !!! // 前置通知
hello,i'm hh // 执行真实方法
after return advice method execute !!!  // 后置通知
after advice method execute !!!  // 最终通知
Around advice after method execute !!! // 环绕通知

3. 总结

AOP的使用很简单,理解"基本概念"后,使用基本不成问题。第一次接触的小伙伴,可以先略读概念,学会使用后,再回顾精读、理解概念,效果更佳。

相关推荐
春生野草27 分钟前
关于SpringMVC的整理
spring
Bug退退退1231 小时前
RabbitMQ 高级特性之重试机制
java·分布式·spring·rabbitmq
hello早上好2 小时前
CGLIB代理核心原理
java·spring
先睡8 小时前
Redis的缓存击穿和缓存雪崩
redis·spring·缓存
Bug退退退12313 小时前
RabbitMQ 高级特性之死信队列
java·分布式·spring·rabbitmq
booooooty19 小时前
基于Spring AI Alibaba的多智能体RAG应用
java·人工智能·spring·多智能体·rag·spring ai·ai alibaba
极光雨雨19 小时前
Spring Bean 控制销毁顺序的方法总结
java·spring
Spirit_NKlaus19 小时前
解决HttpServletRequest无法获取@RequestBody修饰的参数
java·spring boot·spring
lwb_011820 小时前
SpringCloud——Gateway新一代网关
spring·spring cloud·gateway
lxsy1 天前
spring-ai-alibaba 1.0.0.2 学习(七)——集成阿里云百炼平台知识库
学习·spring·阿里云·spring-ai·ai-alibaba