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

相关推荐
终末圆4 小时前
MyBatis XML映射文件编写【后端 18】
xml·java·开发语言·后端·算法·spring·mybatis
憨憨憨憨憨到不行的程序员5 小时前
Spring框架基础知识
java·后端·spring
咖啡攻城狮Alex6 小时前
Spring在不同类型之间也能相互拷贝?
java·后端·spring
姜西西_12 小时前
[Spring]Spring MVC 请求和响应及用到的注解
java·spring·mvc
dawn19122812 小时前
SpringMVC 入门案例详解
java·spring·html·mvc
极客先躯12 小时前
高级java每日一道面试题-2024年9月16日-框架篇-Spring MVC和Struts的区别是什么?
java·spring·面试·mvc·struts2·框架篇·高级java
尘浮生15 小时前
Java项目实战II基于Java+Spring Boot+MySQL的大型商场应急预案管理系统(源码+数据库+文档)
java·开发语言·数据库·spring boot·spring·maven·intellij-idea
尘浮生15 小时前
Java项目实战II基于Java+Spring Boot+MySQL的校园社团信息管理系统(源码+数据库+文档)
java·开发语言·数据库·spring boot·mysql·spring·maven
尘浮生17 小时前
Java项目实战II基于Java+Spring Boot+MySQL的作业管理系统设计与实现(源码+数据库+文档)
java·开发语言·数据库·spring boot·后端·mysql·spring
拉玛干1 天前
社团周报系统可行性研究-web后端框架对比-springboot,django,gin
数据库·python·spring·golang