络腮胡菲菲 发表于 2025-4-1 22:07:06

11-SpringBoot3入门-整合aop

1、概念(个人理解)

AOP(Aspect Oriented Programming),面向切面编程。
1)切面(Aspect):提供切入毗连点的方法
2)毗连点(Joinpoint):即被切入的方法
2、整合

1)添加依靠

      <!--aop-->
      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
      </dependency> 2)创建切面类(Aspect)

比如在service层的所有类的所有方法实行前打印日记信息。
https://i-blog.csdnimg.cn/direct/2b8b443b9acb41869cf2388df0970e1c.png
package com.sgu.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
* 满堂花醉三千客,一剑寒霜十四州。
*
* @Author 中瑞
* @Date 2025/3/31 20:56
*/

@Component
@Aspect
public class logAdvice {
    // 在service层的所有类的所有方法执行前打印日志信息
        @Before("execution(* com..service.*.*(..))")
        public void before(JoinPoint joinPoint){
                System.out.println("LogAdvice.before");
                System.out.println("joinPoint = " + joinPoint);
        }
}
 https://i-blog.csdnimg.cn/direct/f5eaeb7d87c24d0cb86a86f71cb5ac9c.png
控制台输出
https://i-blog.csdnimg.cn/direct/10a7b586ede547b18811c4ffaf9eae7f.png 3、参考

   150-springboot-aop和tx整合和配置_哔哩哔哩_bilibili

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 11-SpringBoot3入门-整合aop