Spring学习04-[Spring容器核心技术AOP学习]

AOP学习

AOP介绍


  • 如何在Spring中创建一个所谓切面?
    @Aspect+@Component+通知+切点
  • 切面里面的代码怎么运行在业务方法(之前、之后)?
    通知+切点

使用

实现一个对service方法的增强,添加日志

  • 添加依赖
xml 复制代码
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

对业务方法添加计算时间的增强

  • 业务类
java 复制代码
@Service
public class UserService {

    public void add(){
        System.out.println("增加");
    }

    public void delete(){
        System.out.println("删除");
    }

    public void update(){
        System.out.println("修改");
    }

    public void query(){
        System.out.println("查询");
    }
}
  • 切面类
    啥叫面向切面编程:面向切面编程就是面向切面类进行编程
java 复制代码
@Aspect //标记为切面类
@Component
public class LogAspect {


    //实现方法用时 切点表达式
    @Around("execution(* com.sping.service.UserService.*(..))")
    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
        //记录方法用时
        long startTime = System.currentTimeMillis();
        //执行具体的方法
        try {
            joinPoint.proceed(); //连接点就是具体的方法
        }catch (Exception e){
            System.out.println("方法执行异常");
            throw new Exception();
        }

        long endTime = System.currentTimeMillis();
        System.out.println("方法用时:" + (endTime - startTime) + "ms");
        return null;
    }


}
  • 测试
java 复制代码
  @Autowired
    private UserService userService;

    @Test
    public void test() throws InterruptedException {
        userService.add();
    }

@EnableAspectJAutoProxy


AOP的术语





切面、切点、通知、连接点;顾问(通知+切点)

通知

  • 前置通知
  • 后置通知
  • 返回通知
  • 异常通知
    在定义切点的时候,可以把切点定义到另一个方法中,然后在通知中引入即可
java 复制代码
@Aspect
@Component
public class LogAspect{

    @Pointcut("execution( * com.sping.service.UserService.*(..))")
    public void PointCut(){

    }


    @AfterReturning("PointCut()")
    public void log(){
        System.out.println("后置通知执行");
    }
}

环绕通知和其他通知的区别:环绕通知需要手动去拿到连接点执行目标方法,环绕方法更加的灵活

前置通知@Before

在目标方法执行之前执行

java 复制代码
@Aspect
@Component
public class LogAspect{

    @Before("execution(* com.sping.service.*.*(..))")
    public void log(){
        System.out.println("前置通知执行");
    }
}

可以看到标注了前置通知后,前置通知的增强方法先执行,然后再执行目标方法

后置通知@After

与前置通知相反,后置通知是目标方法执行之后才会执行,通知里的增强方法

java 复制代码
@Aspect
@Component
public class LogAspect{
    @After("execution(* com.sping.service.*.*(..))")
    public void log(){
        System.out.println("后置通知执行");
    }
}

返回通知@AfterReturning

其实也是个后置通知,不过就是在方法返回后执行

相关推荐
bingbingyihao1 小时前
多数据源 Demo
java·springboot
在努力的前端小白6 小时前
Spring Boot 敏感词过滤组件实现:基于DFA算法的高效敏感词检测与替换
java·数据库·spring boot·文本处理·敏感词过滤·dfa算法·组件开发
一叶飘零_sweeeet8 小时前
从繁琐到优雅:Java Lambda 表达式全解析与实战指南
java·lambda·java8
好望角雾眠9 小时前
第一阶段C#基础-10:集合(Arraylist,list,Dictionary等)
笔记·学习·c#
艾伦~耶格尔9 小时前
【集合框架LinkedList底层添加元素机制】
java·开发语言·学习·面试
星仔编程9 小时前
python学习DAY46打卡
学习
一只叫煤球的猫9 小时前
🕰 一个案例带你彻底搞懂延迟双删
java·后端·面试
最初的↘那颗心9 小时前
Flink Stream API 源码走读 - print()
java·大数据·hadoop·flink·实时计算
大霞上仙9 小时前
实现自学习系统,输入excel文件,能学习后进行相应回答
python·学习·excel
JH307310 小时前
Maven的三种项目打包方式——pom,jar,war的区别
java·maven·jar