SpringBoot 自定义注解及 AOP 的开发和使用

在公司项目中,如果需要做一些公共的功能,如日志等,最好的方式是使用自定义注解,自定义注解可以实现我们对想要添加日志的方法上添加,这篇文章基于日志功能来讲讲自定义注解应该如何开发和使用。

一、引入 AOP 依赖

自定义注解一般会和AOP(切面)结合使用,所以我们首先需要在项目中引入相应的依赖。

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

二、创建自定义注解Log

annotation.Log:

java 复制代码
import java.lang.annotation.*;  
  
@Target(ElementType.METHOD)  
@Retention(RetentionPolicy.RUNTIME)  
@Documented  
public @interface Log {  
    String value() default "";  
}
  • @Target: 表明该注解可以作用的目标是谁,也就是你的注解是用来修饰方法?类?还是属性?这里是表明作用在方法上。
  • @Retention: 表明该注解作用的生命周期,这里表明在运行时有效。
  • @Documented: 表明被它修饰的注解将被javadoc提取成文档。

三、创建AOP切面类

aspect.LogAspect:

java 复制代码
import org.aspectj.lang.ProceedingJoinPoint;  
import org.aspectj.lang.annotation.*;  
import org.springframework.stereotype.Component;  
  
@Aspect  
@Component  
public class LogAspect {  
    @Pointcut("@annotation(com.jk.annotation.Log)")  
    public void pointCut(){}  

    @Before("pointCut()")  
    public void before() {  
        System.out.println("前置通知...");  
    }  

    @After("pointCut()")  
    public void after() {  
        System.out.println("后置通知...");  
    }  

    @Around("pointCut()")  
    public void around(ProceedingJoinPoint point) throws Throwable {  
        System.out.println("环绕通知前...");  
        point.proceed();  
        System.out.println("环绕通知后...");  
    }  
}
  • @Aspect: 表明该类为切面类,定义切面类时必须加这个注解。
  • @Component: 表明把该类交给IOC容器控制。
  • @Pointcut: 定义切入点,结合@annotation(@interface)使用,表明该方法为切入点。@interface必须写自定义注解的全路径名。
  • @Before: 表明被自定义注解代理的方法执行前,先调用的方法。
  • @After: 表明被自定义注解代理的方法执行后,return前调用的方法。
  • @Around: 将被自定义注解代理的方法封装起来,环绕通知(即在他之前之后通知),point.proceed()表明执行的目标方法。

四、自定义注解测试

controller.TestController:

java 复制代码
import com.jk.annotation.Log;  
import org.springframework.web.bind.annotation.GetMapping;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RestController;  
  
@RestController  
@RequestMapping("/test")  
public class TestController {  
    @GetMapping  
    @Log  
    public void test() {  
        System.out.println("执行test方法");
    }  
}

test()方法上添加了一个@Log注解,他会在执行这个方法时,执行我们之前定义切面时创建的前置方法、后置方法、环绕方法。

SpringBoot中使用自定义注解就是如此简单,一般在通知方法中我们还会结合反射来获取执行方法的一些信息,如方法名,参数,响应值等,在后面我也会新开一篇文章专门讲讲反射,有兴趣的掘友可以关注一下哦!

相关推荐
西瓜er25 分钟前
JAVA:Spring Boot 集成 FFmpeg 实现多媒体处理
java·spring boot·ffmpeg
你总是一副不开心的样子(´ . .̫ .28 分钟前
一、十天速通Java面试(第三天)
java·面试·职场和发展·java面试
迎風吹頭髮39 分钟前
UNIX下C语言编程与实践63-UNIX 并发 Socket 编程:非阻塞套接字与轮询模型
java·c语言·unix
我是华为OD~HR~栗栗呀41 分钟前
23届考研-Java面经(华为OD)
java·c++·python·华为od·华为·面试
Javatutouhouduan1 小时前
Java程序员如何深入学习JVM底层原理?
java·jvm·java面试·后端开发·java架构师·java程序员·互联网大厂
王嘉俊9251 小时前
设计模式--享元模式:优化内存使用的轻量级设计
java·设计模式·享元模式
2301_803554522 小时前
C++联合体(Union)详解:与结构体的区别、联系与深度解析
java·c++·算法
EnCi Zheng2 小时前
SpringBoot 配置文件完全指南-从入门到精通
java·spring boot·后端
烙印6012 小时前
Spring容器的心脏:深度解析refresh()方法(上)
java·后端·spring
为什么我不是源代码2 小时前
JPA读取数据库离谱问题-No property ‘selectClassByName‘ found-Not a managed type
java·sql