本文主要参考了Spring官方文档第10章以及第11章和第40章的部分内容。如果要我总结Spring AOP的作用,不妨借鉴文档里的一段话:One of the key components of Spring is the AOP framework. While the Spring IoC container does not depend on AOP, meaning you do not need to use AOP if you don’t want to, AOP complements Spring IoC to provide a very capable middleware solution.(译:如果你不想使用AOP完全可以忽略,只单独使用IoC。但是作为重点,Spring AOP框架是对IoC的有力补充)。

***************************以下是正文部分***************************

官方文档对AOP的讲述有点复杂,我稍微修改了一下组织结构,试图从实用性的角度介绍Spring AOP。有些词汇的译法并没有采用公认的翻译。

一、几个重要的概念

切面(Aspect):能够嵌入多个类的模块。Spring AOP框架使用普通类实现切面。

连接点(Join point):正常业务流上的作用点,例如一个方法或一段异常处理。

建言(Advice):在切点上执行的方法。不同类型的建言包括:"around", "before" 和 "after"。

切点(Pointcut):代表了嵌入模块上的作用点。只有切点上才能运行建言。

AOP代理(AOP proxy):SpringAOP通常采用JDK Dynamic Proxy或CGLIB两种方式实现AOP代理。

织入(Weaving):织入的概念是所有AOP框架所共有的。它指将切面对象与建言对象连接的行为。织入行为能够始于编译期(compile time)、加载期(load time)或运行期(runtime)。Spring AOP的织入是在运行期。

二、对于@AspectJ的支持

Spring AOP框架支持@AspectJ风格的注解声明。

AspectJ也是一种AOP框架,它是对JVM的一种嵌入式开发。AspectJ的AOP织入始于编译期。Spring AOP框架仅借鉴了AspectJ提供的注解方案,因此织入依然是在运行期完成的。

(1)通过Maven添加依赖

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>4.2.2.RELEASE</version>
</dependency>
maven依赖

相关文章:

  • 2021-05-24
  • 2022-01-18
  • 2021-12-21
  • 2022-03-09
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-11
  • 2021-09-26
  • 2021-05-14
  • 2021-12-24
  • 2021-06-15
  • 2021-12-05
  • 2021-11-23
相关资源
相似解决方案