一、常用注解
1.AOP核心概念
AOP (Aspect Orient Programming),直译过来就是 面向切面编程,AOP 是一种编程思想,是面向对象编程(OOP)的一种补充。
2.AOP通知分类注解
@Aspect(切面类) | 用于类上方,标明这是一个Aop类(切面类) |
@Pointcut(切点) | 用于标明在哪些方法上增强 |
@Before(前置通知) | 在方法执行前,执行 |
@After(后置通知) | 在方法执行后,执行 |
@Around(环绕通知) | 在方法前和后,分别执行 |
@AfterReturning(返回后通知) | 在获取返回值后,执行 |
@AfterThrowing(抛出异常通知) | 在抛出异常后,执行 |
二、使用案例
0.添加依赖
<!-- aop依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.16</version>
</dependency>
1.自定义注解
package com.example.sqlitetest.annotate;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author szh
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AopAnnotate {
}
2.切面类
package com.example.sqlitetest.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.Arrays;
/**
* @author szh
*/
@Aspect
@Component
public class AopAspect {
/**
* 定义了一个切点
* 这里的路径填自定义注解的全路径
*/
@Pointcut("@annotation(com.example.sqlitetest.annotate.AopAnnotate)")
public void aopCut() {
}
/**
* 前置通知,在方法运行前输出
*
* @param joinPoint 连接点
*/
@Before("aopCut()")
public void cutProcess(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
System.out.println("注解方式AOP开始拦截, 当前拦截的方法名: " + method.getName());
System.out.println(Arrays.toString(joinPoint.getArgs()));
}
/**
* 后置通知,在方法运行后输出
*
* @param joinPoint 连接点
*/
@After("aopCut()")
public void after(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
System.out.println("注解方式AOP执行的方法 :" + method.getName() + " 执行完了");
}
/**
* 环绕通知,在方法运行前和后分别输出
* joinPoint.proceed() 表示方法运行
*
* @param joinPoint 连接点
*/
@Around("aopCut()")
public Object testCutAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("注解方式AOP拦截开始进入环绕通知.......");
Object proceed = joinPoint.proceed();
System.out.println("准备退出环绕......");
return proceed;
}
/**
* returning属性指定连接点方法返回的结果放置在result变量中
* AfterReturning 获取到返回值后输出
*
* @param joinPoint 连接点
* @param result 返回结果
*/
@AfterReturning(value = "aopCut()", returning = "result")
public void afterReturn(JoinPoint joinPoint, Object result) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
System.out.println("注解方式AOP拦截的方法执行成功, 进入返回通知拦截, 方法名为: " + method.getName() + ", 返回结果为: " + result.toString());
}
/**
* returning属性指定连接点方法返回的结果放置在result变量中
* AfterThrowing 抛出异常后数据
*
* @param joinPoint 连接点
* @param e 异常结果
*/
@AfterThrowing(value = "aopCut()", throwing = "e")
public void afterThrow(JoinPoint joinPoint, Exception e) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
System.out.println("注解方式AOP进入方法异常拦截, 方法名为: " + method.getName() + ", 异常信息为: " + e.getMessage());
}
}
3.测试类(Controller)
package com.example.sqlitetest.controller;
import com.example.sqlitetest.annotate.AopAnnotate;
import com.example.sqlitetest.result.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author szh
*/
@RestController
@RequestMapping("/aop")
public class AopController {
@GetMapping("/aopTest")
@AopAnnotate
public R aopTest(@RequestParam String name) {
// 远程调用
System.out.println("正在执行接口name" + name);
// 模拟异常
// int i = 1 / 0;
return R.success("执行成功" + name);
}
}
以上就是SpringAop的快速入门的方法。
欢迎批评,指正。