springboot实现aop

简介: springboot实现aop

AOP(术语)

  1. 连接点
    类里面哪些方法可以增强,这些点被称为连接点
  2. 切入点
    实际被真正增强的方法
  3. 通知(增强)
    实际增强的逻辑部分称为通知(增强)
    通知(增强)有多种类型
  • 前置通知–@Before
  • 后置通知–@After
  • 环绕通知–@Around
  • 异常通知–@AfterThrowing
  • 最终通知–@AfterReturning
    切面(是动作)–
    把通知(增强)应用到切入点过程

引入依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <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>
    </dependencies>

实现步骤

  1. 新建一个注解类,用于后面做切入点
@Retention(value = RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Authorization {
}
  1. 建一个切面类
@Aspect
@Component
public class AuthAspect {
    /**
     * 定义了一个切点
     * 这里的路径填自定义注解的全路径
     */
    @Pointcut("@annotation(com.zhuyh.studytest.spring5.aop.Authorization)")
    public void authornizeCut() {
    }
    @Before("authornizeCut()")
    public void cutProcess(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("AOP开始拦截, 当前拦截的方法名: " + method.getName());
    }
    @After("authornizeCut()")
    public void after(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("AOP执行的方法 :" + method.getName() + " 执行完了");
    }
    @Around("authornizeCut()")
    public Object testCutAround(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("AOP拦截开始进入环绕通知.......");
        Object proceed = joinPoint.proceed();
        System.out.println("准备退出环绕......");
        return proceed;
    }
    /**
     * returning属性指定连接点方法返回的结果放置在result变量中
     *
     * @param joinPoint 连接点
     * @param result    返回结果
     */
    @AfterReturning(value = "authornizeCut()", 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());
    }
    @AfterThrowing(value = "authornizeCut()", 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());
    }
}
  1. 来一个Controller作为请求被代理的对象
@RestController
@RequestMapping("/company")
public class CompanyController {
    @GetMapping("/aopTest")
    @Authorization
    public Object aopTest(@RequestParam String name){
        //远程调用
        System.out.println("执行接口name:" + name);
        // int n = 1/0;
        return "成功了-----" + name;
    }
}
  1. 启动类启动项目
@SpringBootApplication
public class StudyTestApplication {
    public static void main(String[] args) {
        SpringApplication.run(StudyTestApplication.class, args);
    }
}

测试验证

  1. 浏览器或者api调用工具调接口
  2. 控制台打印
AOP拦截开始进入环绕通知.......
AOP开始拦截, 当前拦截的方法名: aopTest
执行接口name:zhuyh
AOP拦截的方法执行成功, 进入返回通知拦截, 方法名为: aopTest, 返回结果为: 成功了-----zhuyh
AOP执行的方法 :aopTest 执行完了
准备退出环绕......
  1. Controller加入一个异常 int i=1/0; 重启项目再次调用
    异常通知打印了 / by zero
AOP拦截开始进入环绕通知.......
AOP开始拦截, 当前拦截的方法名: aopTest
执行接口name:zhuyh
AOP进入方法异常拦截, 方法名为: aopTest, 异常信息为: / by zero
AOP执行的方法 :aopTest 执行完了
2024-01-24 16:50:10.477 ERROR 4888 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.ArithmeticException: / by zero] with root cause
java.lang.ArithmeticException: / by zero
  at com.zhuyh.studytest.spring5.aop.CompanyController.aopTest(CompanyController.java:16) ~[classes/:na]

感谢阅读

谢谢您的陪伴! 如果您有任何问题、建议或想要了解的特定主题,请随时在评论中告诉我们。期待与您共同探索java,共同提升我们的Java开发技能!

相关文章
|
1月前
|
SQL 监控 Java
在IDEA 、springboot中使用切面aop实现日志信息的记录到数据库
这篇文章介绍了如何在IDEA和Spring Boot中使用AOP技术实现日志信息的记录到数据库的详细步骤和代码示例。
在IDEA 、springboot中使用切面aop实现日志信息的记录到数据库
|
2月前
|
Java Spring
在Spring Boot中使用AOP实现日志切面
在Spring Boot中使用AOP实现日志切面
|
1月前
|
Java Spring 容器
SpringBoot整合AOP实现打印方法执行时间切面
SpringBoot整合AOP实现打印方法执行时间切面
30 1
|
1月前
|
Java API Spring
Spring Boot 中的 AOP 处理
对 Spring Boot 中的切面 AOP 做了详细的讲解,主要介绍了 Spring Boot 中 AOP 的引入,常用注解的使用,参数的使用,以及常用 api 的介绍。AOP 在实际项目中很有用,对切面方法执行前后都可以根据具体的业务,做相应的预处理或者增强处理,同时也可以用作异常捕获处理,可以根据具体业务场景,合理去使用 AOP。
|
2月前
|
Java Spring
在Spring Boot中使用AOP实现日志切面
在Spring Boot中使用AOP实现日志切面
|
2月前
|
XML 监控 Java
如何在Spring Boot中使用AOP
如何在Spring Boot中使用AOP
|
2月前
|
监控 Java Spring
在Spring Boot中使用AOP实现日志记录
在Spring Boot中使用AOP实现日志记录
|
NoSQL Java Redis
SpringBoot - AOP之登录身份验证
SpringBoot - AOP之登录身份验证
340 0
|
3天前
|
前端开发 JavaScript Java
基于Java+Springboot+Vue开发的音乐推荐管理系统
基于Java+Springboot+Vue开发的音乐推荐管理系统(前后端分离),这是一项为大学生课程设计作业而开发的项目。该系统旨在帮助大学生学习并掌握Java编程技能,同时锻炼他们的项目设计与开发能力。通过学习基于Java的音乐推荐管理系统项目,大学生可以在实践中学习和提升自己的能力,为以后的职业发展打下坚实基础。
41 8
基于Java+Springboot+Vue开发的音乐推荐管理系统
|
3天前
|
前端开发 JavaScript Java
基于Java+Springboot+Vue开发的母婴商城管理系统
基于Java+Springboot+Vue开发的母婴商城管理系统(前后端分离),这是一项为大学生课程设计作业而开发的项目。该系统旨在帮助大学生学习并掌握Java编程技能,同时锻炼他们的项目设计与开发能力。通过学习基于Java的网上母婴商城管理系统项目,大学生可以在实践中学习和提升自己的能力,为以后的职业发展打下坚实基础。
22 7
基于Java+Springboot+Vue开发的母婴商城管理系统