• (续)SSM整合之spring笔记(AOP 基于注解的AOP之准备工作)(P100)


    基于注解的AOP之准备工作

    一 . 技术说明

    动态代理( InvocationHandler ): JDK 原生的实现方式,需要被代理的目标类必须实现接口。因
    为这个技术要求 代理对象和目标对象实现同样的接口 (兄弟两个拜把子模式)。
    cglib :通过 继承被代理的目标类 (认干爹模式)实现代理,所以不需要目标类实现接口。
    AspectJ :本质上是静态代理, 将代理逻辑 织入 被代理的目标类编译得到的字节码文件 ,所以最
    终效果是动态的。 weaver 就是织入器。 Spring 只是借用了 AspectJ 中的注解。

    二  .  准备工作

    ①  创建模块spring-aop   com.atguigu.spring

    ②  添加依赖 

    IOC 所需依赖基础上再加入下面依赖即可:
    
    
        org.springframework
        spring-aspects
        5.3.1
    
    完整的依赖  
    jar
    
    
        
        
            org.springframework
            spring-context
            5.3.1
        
        
        
            junit
            junit
            4.12
            test
        
        
        
            org.springframework
            spring-aspects
            5.3.1
        
    

    ③  准备被代理的目标资源

    com.atguigu.spring.aop.annotation  

    把模块spring-proxy里面的Calculator和CalculatorImpl复制到spring-aop模块的com.atguigu.spring.aop.annotation中

    具体代码如下:

    接口: Calculator
    1. package com.atguigu.spring.aop.annotation;
    2. public interface Calculator {
    3. int add(int i, int j);
    4. int sub(int i, int j);
    5. int mul(int i, int j);
    6. int div(int i, int j);
    7. }

    实现类: CalculatorPureImpl

    1. package com.atguigu.spring.aop.annotation;
    2. public class CalculatorImpl implements Calculator {
    3. @Override
    4. public int add(int i, int j) {
    5. int result = i + j;
    6. System.out.println("方法内部,result:"+result);
    7. return result;
    8. }
    9. @Override
    10. public int sub(int i, int j) {
    11. int result = i - j;
    12. System.out.println("方法内部,result:"+result);
    13. return result;
    14. }
    15. @Override
    16. public int mul(int i, int j) {
    17. int result = i * j;
    18. System.out.println("方法内部,result:"+result);
    19. return result;
    20. }
    21. @Override
    22. public int div(int i, int j) {
    23. int result = i / j;
    24. System.out.println("方法内部,result:"+result);
    25. return result;
    26. }
    27. }

    三 . 创建切面类并配置  

    1 . 创建切面类LogAspect

    2 . 创建配置类aop-annotation.xml

    第一步:

    第二步:

     第三步:  (1)把目标类CalculatorImpl交给IOC管理  因为这个类不是三层的哪一层 所以直接用@Component

    1. package com.atguigu.spring.aop.annotation;
    2. import org.springframework.stereotype.Component;
    3. /**
    4. * Date:2022/7/4
    5. * Author:ybc
    6. * Description:
    7. */
    8. @Component
    9. public class CalculatorImpl implements Calculator {
    10. @Override
    11. public int add(int i, int j) {
    12. int result = i + j;
    13. System.out.println("方法内部,result:"+result);
    14. return result;
    15. }
    16. @Override
    17. public int sub(int i, int j) {
    18. int result = i - j;
    19. System.out.println("方法内部,result:"+result);
    20. return result;
    21. }
    22. @Override
    23. public int mul(int i, int j) {
    24. int result = i * j;
    25. System.out.println("方法内部,result:"+result);
    26. return result;
    27. }
    28. @Override
    29. public int div(int i, int j) {
    30. int result = i / j;
    31. System.out.println("方法内部,result:"+result);
    32. return result;
    33. }
    34. }

     (2)把切面类(LogAspect)交给IOC管理    因为这个类不是三层的哪一层 所以直接用@Component

    1. package com.atguigu.spring.aop.annotation;
    2. import org.springframework.stereotype.Component;
    3. @Component
    4. public class LogAspect {
    5. }
     
     

     第四步:扫描  

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    6. <context:component-scan base-package="com.atguigu.spring.aop.annotation"/>
    7. beans>

    以上执行是切面类(LogAspect)和目标类(CalculatorImpl)都需要交给IOC容器管理

    第五步: 加上@Aspect将当前组件标识为切面

    因为对于IOC容器来说   @Component是一个普通的组件   现在要告诉IOC 他是一个切面组件

    所以这时我们要加上@Aspect将当前组件标识为切面

     

    1. package com.atguigu.spring.aop.annotation;
    2. import org.aspectj.lang.annotation.Aspect;
    3. import org.springframework.stereotype.Component;
    4. @Component
    5. @Aspect
    6. public class LogAspect {
    7. }
     
     

     

     
     

     
     

     

     

     

  • 相关阅读:
    Small Tools(5) 前端项目搭建:Vue3+Vite2+TypeScript+Vue Router+Element Plus+Pinia
    UE5 Sequencer 使用指导 - 学习笔记
    XYplorer 23多语言,最好的管理软件之一
    力扣 -- 44. 通配符匹配
    HOOPS/MVO技术概述
    介绍一个开源IOT组态项目
    【Git 】常用指令
    设计模式Java实战
    net.schmizz.sshj.DefaultConfig Illegal key size问题,NIFI部分版本因此无法正常启动
    使用easyexcel将csv转为excel
  • 原文地址:https://blog.csdn.net/m0_59281987/article/details/127718537