Spring 面向切面编程AOP 详细讲解

2026-01-17 20:13:53

1. Spring 面向切面编程AOP 详细讲解

@

目录1. Spring 面向切面编程AOP 详细讲解每博一文案2. AOP介绍说明2.1 AOP的七大术语2.2 AOP 当中的 切点表达式3. 使用Spring 对 AOP 的实现使用3.1 准备工作3.2 Spring 基于AspectJ的AOP注解式开发3.2.1 实现步骤3.2.2 各个通知类型的说明3.2.3 切面的先后顺序的设置3.2.4 优化切点表达式的方式3.2.5 Spring 全注解式开发ACP3.3 Spring 基于XML配置方式的AOP(了解)3.4 AOP 的实际案例:事务处理3.5 AOP 的实际案例:安全日志4. 总结:5. 最后:

每博一文案

你逢人就炫耀的玫瑰,枯萎时该如何收场

我炫耀过的玫瑰永远都不会枯萎

可能有一天它选择了更好的土壤

但仍然会在我这里留下芳香

当我炫耀它的时候

就没想过束缚它

它开的更美,更艳才是我的初衷

Spring IOC 是软件组件松耦合度,而AOP让你能够捕捉系统中经常使用的功能,把它转化为组件。

AOP(Aspect Oriented Programming):面向切面编程 ,面向方面编程 。(AOP是一种编程技术)

AOP 是对OOP的补充延申。

AOP底层使用的就是动态代理,关于动态代理,想要了解更多的,大家可以移步至:✏️✏️✏️ GoF之代理模式(静态代理+动态代理(JDK动态代理+CGLIB动态代理带有一步一步详细步骤))-CSDN博客

Spring 的AOP使用的是动态代理是: JDK动态代理 + CGLIB 动态代理技术 。Spring 在这两种动态代理中灵活切换,如果是代理接口,会默认使用JDK动态代理,如果要代理某个类,这个类没有实现接口,就会切换使用CGLIB。当然,你也可以强制通过一些配置让Spring 只使用 CGLIB。(让文章内容有所说明)

2. AOP介绍说明

一般一个系统当中都会有一些系统服务,例如:日志,事务处理,安全等,这些系统服务被称为:交叉业务 。这些交叉业务 几乎是通用的,不管你是做银行账户转账,还是删除用户数据。日志,事务处理,安全,这些都是需要做的。

如果在每一个业务处理过程当中,都参杂这些交叉业务 代码进去的话,存在两方面的问题:

第一:交叉业务代码在多个业务流程中反复出现,显然这个交叉业务 代码并没有得到充分的复用,并且修改这些交叉业务 代码的话,需要修改多处。

第二:程序员无法专注核心业务代码的编写,在编写核心业务代码的同时还需要处理这些交叉业务。

使用 AOP 可以很轻松的解决以上问题。

如下图:可以更好的理解 AOP思想。

简单的说AOP:就是将与核心业务 无关的代码抽离开来,形成一个独立的组件,然后,以横向 交叉的方式应用到业务流程当中的过程被称为 AOP 。

AOP的优点:

代码的复用性增强

代码易维护

使开发者更关注业务逻辑

2.1 AOP的七大术语

1 连接点 JoinPoint

在程序的整个执行流程中,可以切入 的位置,方法的执行前后,异常抛出之后等位置。

2 切点 Pointcut

在程序执行流程中,真正织入 切面的方法。(一个切点对应多个连接点)

3 通知 Advice

通知叫增强,就是具体你要插入\添加 的代码。

通知包括:

前置通知

后置通知

环绕通知

异常通知

最终通知

4 切面 Aspect :切面 = 切点 + 通知*

5 织入 Weaving

把通知应用到目标对象的过程

6 代理对象 Proxy

一个目标对象被织入通知后产生的新对象

7 目标对象 Target

被织入通知的对象。

2.2 AOP 当中的 切点表达式

所谓的切点表达式:就是用来定义通知(Advice) 往哪些方法上切入的。

切入点表达式语法表达格式:

execution([访问控制权限修饰符] 返回值类型 [全限定类名]方法名(形式参数列表) [异常])

访问控制权限修饰符:

可选项,可以没有

没写,就是默认包括了4个权限

写public 就表示只包括公开的方法。

返回值类型:

必须要有

“*” 表示返回值类型任意

全限定类名

可选项:可以不写

“..” 两个点,表示当前包以及子包下的所有类

省略:表示包括所有的类;就是所有

方法名

必填项

"*" 表示该对应包下的所有任意方法() 就是所有的方法

set* 则表示所有 set 开头的方法()

get* 则表示所有 get 开头的方法()

形式参数列表

必填项

() 空括号,表示没有参数的方法。

(..) 括号中有两个点,表示参数类型和参数个数任意的方法,

(*) 表示只有一个参数的方法

(*,String) 第一个参数类型随意,第二个参数类型必须是String 类型才行

异常:

可选项

省略表示任意异常类型

举例:

execution(public * com.rainbowsea.mall.service.*.delete*(..))

// 表示 public 公开的, * 返回值随意,在 com.rainbowsea.mall.service.* 包下以及子包下的类, delete开头的方法名的方法(参数随意(个数任意,参数类型任意)),的方法。

execution(* com.rainbowsea.mall..*(..))

// 表示 权限符(省略,则默认是包含了4种权限了), * 返回值随意,com.rainbowsea.mall 包下以及子包下的 ..* 任意类,任意方法名,方法中的参数(个数任意,参数类型任意)

execution(* *(..))

// 表示所有的类当中的所有方法,就是所有

3. 使用Spring 对 AOP 的实现使用

Spring 对AOP 的实现包括以下 三种方式:

第一种方式:Spring 框架结合 AspectJ框架实现的AOP,基于注解方式(该比较常用)。

第二种方式:Spring框架结合AspectJ框架实现的AOP,基于XML方式。

第三种方式:Spring 框架自己实现的AOP,基于XML方式。(并不怎么用)

实际开发中,都是Spring + AspectJ实现AOP。所以我们重点学习使用第一种和第二种方式。

什么是AspectJ ? (Eclipse组织的一个支持AOP的框架。AspectJ框架是独立于Spring框架之外的一个框架,Spring框架用了AspectJ)

AspectJ项目起源于帕洛阿尔托(Palo Alto)研究中心(缩写为PARC)。该中心由Xerox集团资助,Gregor Kiczales领导,从1997年开始致力于AspectJ的开发,1998年第一次发布给外部用户,2001年发布1.0 release。为了推动AspectJ技术和社团的发展,PARC在2003年3月正式将AspectJ项目移交给了Eclipse组织,因为AspectJ的发展和受关注程度大大超出了PARC的预期,他们已经无力继续维持它的发展。

3.1 准备工作

使用Spring+AspectJ的AOP需要引入的依赖如下:

repository.spring.milestone

Spring Milestone Repository

https://repo.spring.io/milestone

org.springframework

spring-context

6.0.11

org.springframework

spring-aop

6.0.11

org.springframework

spring-aspects

6.0.11

这边,我多用上了一个 junit4 的注解

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.rainbowsea

spring6-012-aop-realapp

1.0-SNAPSHOT

jar

17

17

repository.spring.milestone

Spring Milestone Repository

https://repo.spring.io/milestone

org.springframework

spring-context

6.0.11

org.springframework

spring-aop

6.0.11

org.springframework

spring-aspects

6.0.11

junit

junit

4.13.2

test

同时需要给:Spring.xml 配置文件中添加 context 命名空间和 aop 命名空间:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

3.2 Spring 基于AspectJ的AOP注解式开发

准备工作,搞定之后,我们就可以进行 Spring 基于 AspectJ的 AOP 注解式开发了。

3.2.1 实现步骤

第一步: 定义好目标类和目标方法。如下:

import org.springframework.stereotype.Service;

@Service(value = "orderService") // 使用 @Service 注解,将该Bean交给 Spring IOC 容器管理

public class OrderService { // 目标对象

// 生成订单业务方法

public void generate() { // 目标方法

System.out.println("正在生成订单...");

}

// 取消订单的业务的方法

public void cancel() { // 目标方法

System.out.println("订单已取消");

}

}

第二步: 定义切面类。

注意: 切面类也是要纳入Spirng IOC 容器当中管理的,因为你是在Spring 框架当中运用的AOP 编程,当然,需要被Spring 管理到,Spring 管理不到,又该让它如何使用呢。

目标类和切面类都纳入spring bean管理

在目标类OrderService上添加@Component 注解。

在切面类MyAspect类上添加@Component 注解。

package com.rainbowsea.spring6.service;

import org.springframework.stereotype.Component;

/**

* 切面类

*/

@Component(value = "myAspect") // 纳入 Spring IOC 容器当中管理

public class MyAspect {

}

第四步: 在spring配置文件中添加组建扫描

所谓的组件扫描:就是让我们里面的注解的位置,简单的说:就是让Spring 框架去哪些包下找,我们的注解,从而管理我们注解下的 Bean 类对象。

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

第五步: 在切面类中添加通知

在切面类当中(对应的类当中)添加通知,就是将 @Aspect 注解,添加到类上就可以了,便会被Spring 框架开启事务通知的操作。

package com.rainbowsea.spring6.service;

import org.aspectj.lang.annotation.Aspect;

import org.springframework.stereotype.Component;

/**

* 切面类

*/

@Component(value = "myAspect") // 纳入 Spring IOC 容器当中管理

@Aspect // 开启事务通知

public class MyAspect {

// 这就是需要增强的代码(通知)

public void advice(){

System.out.println("我是一个通知");

}

}

第六步:在通知上添加切点表达式

在方法当中添加上 @Before() 前置通知注解(详细内容,文章后面说明),想要使用该@Before() 注解的前提是,要在该切面类上添加上 @Aspect 开启事务的注解才行, @Before(execution编写切点表达式)。

package com.rainbowsea.spring6.service;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.springframework.stereotype.Component;

/**

* 切面类

*/

@Component(value = "myAspect") // 纳入 Spring IOC 容器当中管理

@Aspect // 开启事务通知

public class MyAspect {

// 这就是需要增强的代码(通知)

@Before("execution(* com.rainbowsea.spring6.service..*(..))") // 切面表达式:表示:权限符省略,任意返回值,该包下的以及包下的任意方法(参数类型,个人数任意)

public void advice(){

System.out.println("我是一个通知");

}

}

第七步: 在spring配置文件中启用自动代理

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

开启自动代理之后,凡事带有@Aspect注解的bean都会生成代理对象。

proxy-target-class="true" 表示采用cglib动态代理

proxy-target-class="false" 表示采用jdk动态代理。默认值是false。即使写成false,当没有接口的时候,也会自动选择cglib生成代理类。

第八步: 测试程序:

import com.rainbowsea.spring6.service.AccountService;

import com.rainbowsea.spring6.service.OrderService;

import com.rainbowsea.spring6.service.UserService;

import com.rainbowsea.spring6.service.VipService;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AOPRealAppTest {

@Test

public void testAOP() {

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring6.xml");

AccountService accountService = applicationContext.getBean("accountService", AccountService.class);

accountService.transfer();

accountService.withdraw();

}

}

3.2.2 各个通知类型的说明

在Spring 的 AOP当中:有如下五种通知类型:

前置通知:@Before 目标方法执行之前的通知

后置通知:@AfterReturning 目标方法执行之后的通知

环绕通知:@Around 目标方法之前添加通知,通知目标方法执行之后添加通知

异常通知:@AfterThrowing 发生异常之后执行的通知

最终通知:@After 放在 finally 语句块当中的通知

注意:以上五种通知,可以配合上,同时使用上

注意:想要运用通知,需要在对应的切面类上(通知类)上,添加上 @Aspect 注解,开启事务通知,才行 。

前置通知:@Before 目标方法执行之前的通知

前置通知使用: @Before(切面表达式)

import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.springframework.stereotype.Component;

/**

* 切面类

*/

@Component(value = "myAspect") // 纳入 Spring IOC 容器当中管理

@Aspect // 开启事务通知

public class MyAspect {

// 这就是需要增强的代码(通知)

// 前置通知

@Before("execution(* com.rainbowsea.spring6.service..*(..))")

public void beforeAdvice() {

System.out.println("前置通知");

}

}

后置通知:@AfterReturning 目标方法执行之后的通知

前置通知使用: @AfterReturning(切面表达式)

import org.springframework.stereotype.Component;

/**

* 切面类

*/

@Component(value = "myAspect") // 纳入 Spring IOC 容器当中管理

@Aspect // 开启事务通知

public class MyAspect {

// 这就是需要增强的代码(通知)

// 后置通知

@AfterReturning("execution(* com.rainbowsea.spring6.service..*(..))")

public void afterReturningAdvice() {

System.out.println("后置通知");

}

}

环绕通知:@Around 目标方法之前添加通知,通知目标方法执行之后添加通知

环绕通知需要加上:public void aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable,ProceedingJoinPoint joinPoint 参数,同时需要把 异常抛出去,在环绕通知当中需要:自己手动通过 : ProceedingJoinPoint joinPoint.procced() 调用目标方法。才会执行目标方法,不然,是不会执行目标类当中的目标方法的。

// 这个JoinPoint joinPoint ,在Spring容器调用这个方法的时候自动传过来

// 我们可以直接用,用这个 JoinPoint joinPoint 干啥?

// Signature signature = joinPoint.getSignature(); 获取目标方法的签名

// 通过方法的签名可以获取到一个方法的具体信息

// 获取目标方法的方法名

环绕通知使用: @Around(切面表达式)

/**

* 切面类

*/

@Component(value = "myAspect") // 纳入 Spring IOC 容器当中管理

@Aspect // 开启事务通知

public class MyAspect {

// 这就是需要增强的代码(通知)

// 环绕通知(环绕是最大的通知,在前置通知之前,在后置通知之后)

@Around("execution(* com.rainbowsea.spring6.service..*(..))")

public void aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {

// 前面的代码

System.out.println("前环绕");

// 执行目标

joinPoint.proceed(); // 执行目标

// 后面的代码

System.out.println("后环绕");

}

}

异常通知:@AfterThrowing 发生异常之后执行的通知,没发生异常是不会执行的。

异常通知使用: @AfterThrowing(切面表达式)

/**

* 切面类

*/

@Component(value = "myAspect") // 纳入 Spring IOC 容器当中管理

@Aspect // 开启事务通知

public class MyAspect {

// 这就是需要增强的代码(通知)

// 异常通知

@AfterThrowing("execution(* com.rainbowsea.spring6.service..*(..))")

public void afterThrowignAdvice() {

System.out.println("异常通知");

}

}

异常通知:没有异常,就不会执行异常通知的操作。

这里,我们添加模拟上一个 null 指针异常进行,测试异常通知的执行。

最终通知:@After 放在 finally 语句块当中的通知

最终通知使用: @After(切面表达式)。最终通知:无论是否存在异常都是在最后都会执行的。

/**

* 切面类

*/

@Component(value = "myAspect") // 纳入 Spring IOC 容器当中管理

@Aspect // 开启事务通知

public class MyAspect {

// 这就是需要增强的代码(通知)

// 最终通知(finally 语句块中的通知)

@After(("execution(* com.rainbowsea.spring6.service..*(..))"))

public void afterAdvice() {

System.out.println("最终通知");

}

}

有异常,最终通知,也是会执行的。

通过测试得知,当发生异常之后,最终通知也会执行,因为最终通知@After会出现在finally语句块中。

出现异常之后,后置通知和环绕通知的结束部分不会执行。

下面,我们集合五种通知类型,同时发生,看看他们在通知当中的执行顺序又是如何的

package com.rainbowsea.spring6.service;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.After;

import org.aspectj.lang.annotation.AfterReturning;

import org.aspectj.lang.annotation.AfterThrowing;

import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.springframework.stereotype.Component;

/**

* 切面类

*/

@Component(value = "myAspect") // 纳入 Spring IOC 容器当中管理

@Aspect // 开启事务通知

public class MyAspect {

// 这就是需要增强的代码(通知)

// 前置通知

@Before("execution(* com.rainbowsea.spring6.service..*(..))")

public void beforeAdvice() {

System.out.println("前置通知");

}

// 后置通知

@AfterReturning("execution(* com.rainbowsea.spring6.service..*(..))")

public void afterReturningAdvice() {

System.out.println("后置通知");

}

// 环绕通知(环绕是最大的通知,在前置通知之前,在后置通知之后)

@Around("execution(* com.rainbowsea.spring6.service..*(..))")

public void aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {

// 前面的代码

System.out.println("前环绕");

// 执行目标

joinPoint.proceed(); // 执行目标

// 后面的代码

System.out.println("后环绕");

}

// 异常通知

@AfterThrowing("execution(* com.rainbowsea.spring6.service..*(..))")

public void afterThrowignAdvice() {

System.out.println("异常通知");

}

// 最终通知(finally 语句块中的通知)

@After(("execution(* com.rainbowsea.spring6.service..*(..))"))

public void afterAdvice() {

System.out.println("最终通知");

}

}

有异常的:

前环绕

前置通知

银行账户正在完成转账操作...

后置通知

最终通知

后环绕

总结:

环绕通知是范围最大的(也是是说,环绕通知的(前环绕)是在所有通知的最前面执行的,而环绕通知的(后环绕)是在所有通知的最后面执行的)。

前环绕

前置通知

异常通知

最终通知

存在异常时,异常通知执行了,但是后面的:后置通知,环绕通知的(后环绕)通知,并不会执行,被异常给中断了。

3.2.3 切面的先后顺序的设置

我们知道,业务流程当中不一定只有一个切面,可能有的切面控制事务,有的记录日志,有的进安全控制,如果多个切面的话,顺序如何控制:关于这一点:我们可以使用@Order 注解来标识切面类,为@Order注解的 value 指定一个整数型的数字,数字越小,优先级越高。

这里我们分别定义两个通知:分别为 安全方面的通知,和日志方面的通知。如下:

package com.rainbowsea.spring6.service;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.springframework.stereotype.Component;

/**

* 安全事务

*/

@Component // 将该 Bean 交给Spring IOC 容器管理上

@Aspect // 开启事务

public class SecureAspect {

// 前置通知

// 安全

@Before("execution(* com.rainbowsea.spring6.service..*(..))")

public void beforeAdvice() {

System.out.println("安全方面的:前置通知");

}

}

package com.rainbowsea.spring6.service;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.springframework.stereotype.Component;

/**

* 日志通知

*/

@Component // 将该 Bean 加入到 Spring IOC 容器当中管理

@Aspect // 开启事务

public class LogAspect {

// 前置通知

@Before("execution(* com.rainbowsea.spring6.service..*(..))")

public void beforeAdvice() {

System.out.println("日志的通知方面的:前置通知");

}

}

没有添加@Order 注解的通知顺序如下:

我们添加@Order注解的整数值来切换顺序,这里,我们将安全方面的通知,放在最前面执行,执行测试程序:

package com.rainbowsea.spring6.service;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.springframework.core.annotation.Order;

import org.springframework.stereotype.Component;

/**

* 日志通知

*/

@Component // 将该 Bean 加入到 Spring IOC 容器当中管理

@Aspect // 开启事务

@Order(2) // 数值越小,优先级越高,越先执行

public class LogAspect {

// 前置通知

@Before("execution(* com.rainbowsea.spring6.service..*(..))")

public void beforeAdvice() {

System.out.println("日志的通知方面的:前置通知");

}

}

package com.rainbowsea.spring6.service;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.springframework.core.annotation.Order;

import org.springframework.stereotype.Component;

/**

* 安全事务

*/

@Component // 将该 Bean 交给Spring IOC 容器管理上

@Aspect // 开启事务

@Order(1) // 数值越小,优先级越高,越先执行

public class SecureAspect {

// 前置通知

// 安全

@Before("execution(* com.rainbowsea.spring6.service..*(..))")

public void beforeAdvice() {

System.out.println("安全方面的:前置通知");

}

}

3.2.4 优化切点表达式的方式

观看以下代码中的切点表达式:

package com.rainbowsea.spring6.service;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.After;

import org.aspectj.lang.annotation.AfterReturning;

import org.aspectj.lang.annotation.AfterThrowing;

import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.springframework.stereotype.Component;

/**

* 切面类

*/

@Component(value = "myAspect") // 纳入 Spring IOC 容器当中管理

@Aspect // 开启事务通知

public class MyAspect {

// 这就是需要增强的代码(通知)

// 前置通知

// 安全

@Before("execution(* com.rainbowsea.spring6.service..*(..))")

public void beforeAdvice() {

System.out.println("前置通知");

}

// 后置通知

@AfterReturning("execution(* com.rainbowsea.spring6.service..*(..))")

public void afterReturningAdvice() {

System.out.println("后置通知");

}

// 环绕通知(环绕是最大的通知,在前置通知之前,在后置通知之后)

@Around("execution(* com.rainbowsea.spring6.service..*(..))")

public void aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {

// 前面的代码

System.out.println("前环绕");

// 执行目标

joinPoint.proceed(); // 执行目标

// 后面的代码

System.out.println("后环绕");

}

// 异常通知

@AfterThrowing("execution(* com.rainbowsea.spring6.service..*(..))")

public void afterThrowignAdvice() {

System.out.println("异常通知");

}

// 最终通知(finally 语句块中的通知)

@After(("execution(* com.rainbowsea.spring6.service..*(..))"))

public void afterAdvice() {

System.out.println("最终通知");

}

}

上述的缺点是:

第一:切点表达式重复写了多次,没有得到复用。

第二:如果要修改切点表达式,需要修改多处,难维护。

可以这样做:将切点表达式单独的定义出来,在需要的位置引入即可,如下:

我们可以使用:@Pointcut 注解来定义独立的切点表达式。

注意这个 @Pointcut 注解标注的方法随意,只是起到一个能够让@Pointcut注解编写的位置。

package com.rainbowsea.spring6.service;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.After;

import org.aspectj.lang.annotation.AfterReturning;

import org.aspectj.lang.annotation.AfterThrowing;

import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.annotation.Pointcut;

import org.springframework.stereotype.Component;

/**

* 切面类

*/

@Component(value = "myAspect") // 纳入 Spring IOC 容器当中管理

@Aspect // 开启事务通知

public class MyAspect {

// 这就是需要增强的代码(通知)

// 定义通用的切点表达式

@Pointcut("execution(* com.rainbowsea.spring6.service..*(..))")

public void pointcut(){

// 这个方法只是一个标记,方法名随意,方法体也不需要写任何代码。

}

// 前置通知

// 安全

@Before("pointcut()")

public void beforeAdvice() {

System.out.println("前置通知");

}

// 后置通知

@AfterReturning("pointcut()")

public void afterReturningAdvice() {

System.out.println("后置通知");

}

// 环绕通知(环绕是最大的通知,在前置通知之前,在后置通知之后)

@Around("pointcut()")

public void aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {

// 前面的代码

System.out.println("前环绕");

// 执行目标

joinPoint.proceed(); // 执行目标

// 后面的代码

System.out.println("后环绕");

}

// 异常通知

@AfterThrowing("pointcut()")

public void afterThrowignAdvice() {

System.out.println("异常通知");

}

// 最终通知(finally 语句块中的通知)

@After(("pointcut()"))

public void afterAdvice() {

System.out.println("最终通知");

}

}

3.2.5 Spring 全注解式开发ACP

就是编写一个类,在这个类上面使用大量注解来代替 spring.xml的配置文件,spring配置文件消失了,如下:

@Configuration // 代替spring.xml 文件

@ComponentScan(value = {"com.rainbowsea.spring6.service"}) // 组件扫描

@EnableAspectJAutoProxy(proxyTargetClass = true) // 启用 aspectj的自动代理机制

package com.rainbowsea.spring6.service;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration // 代替spring.xml 文件

@ComponentScan(value = {"com.rainbowsea.spring6.service"}) // 组件扫描

@EnableAspectJAutoProxy(proxyTargetClass = true) // 启用 aspectj的自动代理机制

public class Spring6Config {

}

测试程序也变化了:因为我这里是通过定义的一个配置类 代替 spring.xml 文件的需要用的是:new AnnotationConfigApplicationContext(配置类.class),而不再是: new ClassPathXmlApplicationContext()了。

public class AOPRealAppTest {

@Test

public void testAOPWithAllAnnotation() {

ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Spring6Config.class);

AccountService accountService = applicationContext.getBean("accountService", AccountService.class);

accountService.transfer();

}

3.3 Spring 基于XML配置方式的AOP(了解)

第一步: 编写目标类。因为这里我们用的是 xml 配置方式的,不是注解的方式,所有就不用注解了。

package com.rainbowsea.spring6.service;

public class UserService { // 目标对象

public void logout() { // 目标方法

System.out.println("系统正在安全退出...");

}

}

第二步: 编写切面类,并且编写通知

第三步: 编写spring配置文件

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

测试程序:

package com.rainbowsea.spring6.test;

import com.rainbowsea.spring6.service.UserService;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringAOPTest {

@Test

public void testXml() {

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring6.xml");

UserService userService = applicationContext.getBean("userService", UserService.class);

userService.logout();

}

}

3.4 AOP 的实际案例:事务处理

项目中的事务控制是在所难免的。在一个业务流程当中,可能需要多条DML语句共同完成,为了保证数据的安全,这多条DML语句要么同时成功,要么同时失败。这就需要添加事务控制的代码。例如以下伪代码:

class 业务类1{

public void 业务方法1(){

try{

// 开启事务

startTransaction();

// 执行核心业务逻辑

step1();

step2();

step3();

....

// 提交事务

commitTransaction();

}catch(Exception e){

// 回滚事务

rollbackTransaction();

}

}

public void 业务方法2(){

try{

// 开启事务

startTransaction();

// 执行核心业务逻辑

step1();

step2();

step3();

....

// 提交事务

commitTransaction();

}catch(Exception e){

// 回滚事务

rollbackTransaction();

}

}

public void 业务方法3(){

try{

// 开启事务

startTransaction();

// 执行核心业务逻辑

step1();

step2();

step3();

....

// 提交事务

commitTransaction();

}catch(Exception e){

// 回滚事务

rollbackTransaction();

}

}

}

class 业务类2{

public void 业务方法1(){

try{

// 开启事务

startTransaction();

// 执行核心业务逻辑

step1();

step2();

step3();

....

// 提交事务

commitTransaction();

}catch(Exception e){

// 回滚事务

rollbackTransaction();

}

}

public void 业务方法2(){

try{

// 开启事务

startTransaction();

// 执行核心业务逻辑

step1();

step2();

step3();

....

// 提交事务

commitTransaction();

}catch(Exception e){

// 回滚事务

rollbackTransaction();

}

}

public void 业务方法3(){

try{

// 开启事务

startTransaction();

// 执行核心业务逻辑

step1();

step2();

step3();

....

// 提交事务

commitTransaction();

}catch(Exception e){

// 回滚事务

rollbackTransaction();

}

}

}

//......

可以看到,这些业务类中的每一个业务方法都是需要控制事务的,而控制事务的代码又是固定的格式,都是:

try{

// 开启事务

startTransaction();

// 执行核心业务逻辑

//......

// 提交事务

commitTransaction();

}catch(Exception e){

// 回滚事务

rollbackTransaction();

}

这个控制事务的代码就是和业务逻辑没有关系的 “交叉业务” 。以上伪代码当中可以看到这些交叉业务的代码没有得到复用,并且如果这些交叉业务代码需要修改,那必然需要修改多处,难维护,怎么解决?可以采用 AOP 思想解决。可以把以上控制事务的代码作为环绕通知 ,切入到目标类的方法当中,接下来我们做一下这件事,有两个业务类,如下:

package com.rainbowsea.spring6.service;

import org.springframework.stereotype.Component;

import org.springframework.stereotype.Service;

@Component(value = "accountService")

public class AccountService { // 目标对象

// 转账的业务方法

public void transfer() { // 目标方法

System.out.println("银行账户正在完成转账操作...");

}

// 取帐的业务方法

public void withdraw() { // 目标方法

System.out.println("正在取款,请稍后...");

}

}

package com.rainbowsea.spring6.service;

import org.springframework.stereotype.Service;

@Service(value = "orderService") // 使用 @Service 注解,将该Bean交给 Spring IOC 容器管理

public class OrderService { // 目标对象

// 生成订单业务方法

public void generate() { // 目标方法

System.out.println("正在生成订单...");

}

// 取消订单的业务的方法

public void cancel() { // 目标方法

System.out.println("订单已取消");

}

}

注意,以上两个业务类已经纳入spring bean的管理,因为都添加了@Component注解。

接下来我们给以上两个业务类的4个方法添加事务控制代码,使用AOP来完成:

package com.rainbowsea.spring6.service;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Aspect;

import org.springframework.stereotype.Component;

@Component // 交给Spring 管理

@Aspect // 开始AOP

public class TransactionAspect {

// 编程式事务解决方案

@Around("execution(* com.rainbowsea.spring6.service..*(..))") // 环绕通知

public void aroundAdvice(ProceedingJoinPoint joinPoint) {

// 执行目标

try {

// 前环绕

System.out.println("开启事务");

// 执行目标

joinPoint.proceed();

// 后环绕

System.out.println("提交事务");

} catch (Throwable throwable) {

System.out.println("回滚事务");

throwable.printStackTrace();

}

}

}

你看,这个事务控制代码是不是只需要写一次就行了,并且修改起来也没有成本。编写测试程序:

@Test

public void testTransaction() {

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring6.xml");

AccountService accountService = applicationContext.getBean("accountService", AccountService.class);

OrderService orderService = applicationContext.getBean("orderService", OrderService.class);

accountService.transfer();

accountService.withdraw();

orderService.generate();

orderService.cancel();

}

通过测试可以看到,所有的业务方法都添加了事务控制的代码。

关于Spring 对事务的支持使用,由于涉及到的篇幅比较多,大家可以移步至:✏️✏️✏️ (链接)

3.5 AOP 的实际案例:安全日志

需求是这样的:项目开发结束了,已经上线了。运行正常,客户提出了新的需求:凡事在系统中进行修改操作的,删除操作的,新增操作的,都要把这个人记录下来。因为这几个操作是属于危险行为。

package com.rainbowsea.spring6.service;

import org.springframework.stereotype.Service;

@Service(value = "userService") // 被spring 管理

public class UserService {

public void saveUser() {

System.out.println("新增用户信息");

}

public void deleteUser() {

System.out.println("删除用户信息");

}

public void modifyUser() {

System.out.println("修改用户信息");

}

public void getUser() {

System.out.println("获取用户信息");

}

}

package com.rainbowsea.spring6.service;

import org.springframework.stereotype.Service;

@Service(value = "vipService") // 被 spring管理

public class VipService {

public void saveVip() {

System.out.println("新增Vip用户信息");

}

public void deleteVip() {

System.out.println("删除Vip用户信息");

}

public void modifyVip() {

System.out.println("修改Vip用户信息");

}

public void getVip() {

System.out.println("获取Vip用户信息");

}

}

注意:已经添加了@Component注解。

接下来我们使用aop来解决上面的需求:编写一个负责安全的切面类。

这里我们可以联合使用,多个类当中的不同的方法名对应 AOP事务进行也给控制

package com.rainbowsea.spring6.service;

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.annotation.Pointcut;

import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;

import java.util.Date;

@Component(value = "securityLogAspect") // 交给 spring 管理

@Aspect // 开启事务

public class SecurityLogAspect {

// 在 com.rainbowsea.spring6.service..save*() 包下的任意类当中的,save* 开头的任意方法,参数任意

@Pointcut("execution(* com.rainbowsea.spring6.service..save*(..))")

public void savePointcut() {

}

// 在execution(* com.rainbowsea.spring6.service..delete*(..)) 包下的任意类当中的 delete*(..) 开头的任意方法,任意参数

@Pointcut("execution(* com.rainbowsea.spring6.service..delete*(..))")

public void deletePointcut() {

}

// execution(* com.rainbowsea.spring6.service..delete*(..)) 包下的任意类当中的modify*(..) 开头的任意方法,任意参数

@Pointcut("execution(* com.rainbowsea.spring6.service..modify*(..))")

public void modifyPointcut() {

}

// 联合使用,多个类当中的不同的方法名对应 AOP事务

@Before("savePointcut() || deletePointcut() || modifyPointcut()")

public void beforeAdvice(JoinPoint joinPoint) {

// 系统时间

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss SSS");

String nowTime = simpleDateFormat.format(new Date());

// 输出日志信息

System.out.println(nowTime + "zhangsan" + joinPoint.getSignature().getDeclaringTypeName()+

"." + joinPoint.getSignature().getName());

}

}

@Test

public void testSecurityLong() {

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring6.xml");

UserService userService = applicationContext.getBean("userService", UserService.class);

VipService vipService = applicationContext.getBean("vipService", VipService.class);

userService.saveUser();

userService.deleteUser();

userService.modifyUser();

userService.getUser();

vipService.saveVip();

vipService.saveVip();

vipService.modifyVip();

vipService.getVip();

}

4. 总结:

简单的说AOP:就是将与核心业务 无关的代码抽离开来,形成一个独立的组件,然后,以横向 交叉的方式应用到业务流程当中的过程被称为 AOP

AOP的七大术语

AOP 的五种通知(通知叫增强,就是具体你要插入\添加 的代码。)的特点:及其对应的注解

AOP的切点表达式,所谓的切点表达式:就是用来定义通知(Advice) 往哪些方法上切入的。

execution([访问控制权限修饰符] 返回值类型 [全限定类名]方法名(形式参数列表) [异常])

切面类也是要纳入Spirng IOC 容器当中管理的,因为你是在Spring 框架当中运用的AOP 编程,当然,需要被Spring 管理到,Spring 管理不到,又该让它如何使用呢。

所有的五种通知上的使用都必须在该对应的类上,要在该切面类上添加上 @Aspect 开启事务的注解才行。同时也要纳入Spirng IOC 容器当中管理的。才行

五种通知,可以配合上,同时使用上时

环绕通知是范围最大的(也是是说,环绕通知的(前环绕)是在所有通知的最前面执行的,而环绕通知的(后环绕)是在所有通知的最后面执行的)。

存在异常时,异常通知执行了,但是后面的:后置通知,环绕通知的(后环绕)通知,并不会执行,被异常给中断了。

切面的先后顺序的设置:关于这一点:我们可以使用@Order 注解来标识切面类,为@Order注解的 value 指定一个整数型的数字,数字越小,优先级越高。

切点表达式的优化:我们可以使用:@Pointcut 注解来定义独立的切点表达式。

注意这个 @Pointcut 注解标注的方法随意,只是起到一个能够让@Pointcut注解编写的位置。

第一种方式:Spring 框架结合 AspectJ框架实现的AOP,基于注解方式(该比较常用)。

第二种方式:Spring框架结合AspectJ框架实现的AOP,基于XML方式。

关于Spring 在事务上的支持上的使用,由于涉及的篇幅内容过多,所以就不在这里说明了,想要了解更多的可以移步至:✏️✏️✏️

切面类要不仅要加上 开始事务的注解,也要添加上 Spring IOC 容器管理的注解。

5. 最后:

“在这个最后的篇章中,我要表达我对每一位读者的感激之情。你们的关注和回复是我创作的动力源泉,我从你们身上吸取了无尽的灵感与勇气。我会将你们的鼓励留在心底,继续在其他的领域奋斗。感谢你们,我们总会在某个时刻再次相遇。”

如何画铲土机
现在的抖音小店怎么样?到底值不值得做?老司机亲述全过程