SpringBoot 面向切面编程

AOP是Aspect Oriented Programming(面向切面编程)的缩写。可以对方法进行增强,底层使用的是动态代理。

快速上手

环境准备

面向切面编程需要引入相关的依赖,在pom.xml(如果是使用的maven构建的项目的话)文件中引入aop的起步依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

编写AOP程序

在类上加上@Aspect注解,表明这是一个切面类。

通过execution表达式指明对哪些方法进行加强。

@Component
@Aspect
public class RunAsepect  {
    @Around("execution(* com.example.demo.Service.impl.*.*(..))")
    public void Demo (ProceedingJoinPoint proceedingJoinPoint)throws Throwable{
        proceedingJoinPoint.proceed();

    }
}

通知类型

通知类型的注解有五个

@Around环绕通知,该通知方法在目标方法前、后都被执行
@Before前置通知,在目标方法前执行
@Aftrer后置通知,在目标方法后执行,无论是否有异常都执行
@AfterReturning返回后通知,出现异常不执行
@AfterThrowing异常后通知

其中,@Around注解需要在方法中手动调用ProceedingJoinPoint类的proceed()方法调用原始方法。

@AfterReturning可以通过在形参中使用Object类型来接收原始方法的返回值。

通知顺序

默认通知顺序

当有多个切面类的execution表达式匹配到同一个方法时,默认的通知顺序与切面类的类名有关,

对于方法前通知,切面类名排名越靠前的越先执行

对于方法后通知,切面类名排名越靠后的越先执行。

更改通知顺序

除了通过更改切面类的类名更改通知顺序外,可以通过@Order注解来更改通知顺序

对于方法前通知,@Order注解内数字越小的越先执行。

对于方法后通知,@Order注解内数字越大的越先执行

@Order(1)
@Component
@Aspect
public class RunAsepect  {
    @AfterReturning(value = "execution(* com.example.demo.Service.impl.*.*(..))",returning = "result")
    public void Demo (JoinPoint joinPoint,Object result)throws Throwable{
        System.out.println("方法"+joinPoint.getSignature().getName()+"的返回值为"+result);
    }
}

切入点表达式

execution表达式

execution(访问修饰符 返回值 包名.类名.方法名(方法参数) throws 异常)

execution表达式通过访问修饰符,返回值,包名,类名,方法名,异常等进行匹配,

其中:

  • 访问修饰符、包名,类名.、异常都可以省略。

  • 可使用 * 匹配任一结果

  • 可使用 … 可匹配多个连续的任意值。

  • 可通过逻辑运算符(||、&&、!)进行较为复杂的execution表达式匹配。

@annotation表达式

@annotation表达式通过指定注解名进行匹配,配合自定义注解便可以对于一些规则性不强的方法进行匹配。

首先自定义一个注解

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyLog {
}

将该注解加在需要增强的方法上

@Repository
public class Service1 implements MyService {
    @MyLog
    public String run(){
        System.out.println("原始方法已被执行");
        return "这是结果";
    }
}

然后通过@annotation注解对切入点进行描述

@Order(1)
@Component
@Aspect
public class RunAsepect  {
    @AfterReturning(value = "@annotation(com.example.demo.aop.MyLog)",returning = "result")
    //@AfterReturning(value = "execution(* com.example.demo.Service.impl.*.*(..))",returning = "result")
    public void Demo (JoinPoint joinPoint,Object result)throws Throwable{
        System.out.println("方法"+joinPoint.getSignature().getName()+"的返回值为"+result);
    }
}

连接点

Spring中使用JoinPoint抽象了连接点,用它可以获取方法执行时的相关信息。

该类型有以下常用方法

joinPoint.getTarget().getClass().getName();//获得目标类名
joinPoint.getSignature();//获取目标方法签名
joinPoint.getSignature().getName();//获得目标方法名
joinPoint.getArgs();//获得目标方法运行参数

对于@Around类型的通知,由于需要手动地调用原始方法,因此需要使用ProceedingJoinPoint类型来接收。

proceedingJoinPoint.proceed();//调用原始方法
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot 提供了很好的支持来实现面向切面编程面向切面编程(AOP)是一种编程范式,它允许将横跨多个业务逻辑的功能(例如日志记录、事务管理等)从应用程序的核心业务逻辑中分离出来,以提高代码的可重用性、模块化和可维护性。 在 Spring Boot 中,你可以使用 Spring AOP 来实现面向切面编程Spring AOP 基于代理模式,通过在目标对象上动态生成一个代理对象来实现切面功能。Spring AOP 提供了很多注解来定义切面、切入点、通知等。 下面是一个简单的面向切面编程的示例,它演示了如何在 Spring Boot 中使用 AOP 记录方法的执行时间: 1. 定义一个切面类,使用 @Aspect 注解标记,定义一个切入点,使用 @Pointcut 注解标记: ``` @Aspect @Component public class LogAspect { @Pointcut("execution(* com.example.demo.service.*.*(..))") public void servicePointcut() { } @Around("servicePointcut()") public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable { long startTime = System.currentTimeMillis(); Object proceed = joinPoint.proceed(); System.out.println(joinPoint.getSignature().getName() + " executed in " + (System.currentTimeMillis() - startTime) + "ms"); return proceed; } } ``` 2. 在应用程序的启动类上添加 @EnableAspectJAutoProxy 注解来启用 AOP: ``` @SpringBootApplication @EnableAspectJAutoProxy public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 这样,在调用 com.example.demo.service 包下的任何方法时,LogAspect 类中定义的 logExecutionTime 方法都会被调用,记录方法的执行时间。 以上是一个简单的面向切面编程示例。在实际应用中,你可以使用 AOP 来实现很多功能,例如事务管理、安全控制等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

辰宝IWZ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值