• Spring框架-AspectJ配置文件


    一、AOP(底层原理)

    1、AOP底层使用动态代理

    (1)有两种情况动态代理

    第一种 有接口情况,使用JDK动态代理

    第二种 没有接口情况,使用CGLIB动态代理

    AOP(JDK动态代理)

    1、使用JDK动态代理,使用Proxy类里面的方法创建代理对象

    (2)调用newProxyInstance方法

    方法有三个参数:

    第一参数,类加载器

    第二参数,增强方法所在的类,这个类实现的接口,支持多个接口

    第三参数,实现这个接口InvocationHandler,创建代理对象,写增强方法

    2、编写JDK动态代理代码

    (1)创建接口,定义方法

    public interface UserDao {
        //实现相加操作的接口
        public int add(int a,int b);
        public String update(String id);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    (2)创建接口实现类,实现方法

    public class UserDaoImpl implements UserDao {
        public int add(int a, int b) {
            return a+b;
        }
    
        public String update(String id) {
            return id;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2、AOP(术语)

    class User{

    ​ add()

    ​ update()

    ​ select()

    ​ delete()

    }

    1、连接点

    类里面哪些方法可以被增强,这些方法称为连接点

    2、切入点

    实际被真正增强的方法,称为切入点

    3、通知(增强)

    (1)实际增强的逻辑部分称为通知(增强)

    (2)通知有多种类型

    *前置通知

    *后置通知

    *环绕通知

    *异常通知

    *最终通知 : finally

    4、切面

    (1) 把通知应用道切入点过程

    AOP操作(准备)

    1、Spring框架一般都是基于AspectJ实现AOP操作

    (1) 什么是AspectJ

    • AspectJ不是spring组成部分,独立AOP框架,一般把AspectJ和Spring框架一起使用,进行AOP操作

    2、基于AspectJ实现AOP操作

    (1)基于xml配置文件实现

    (2)基于注解方式实现(使用)

    3、在项目工程里面引入AOP相关依赖

    4、切入点表达式

    (1)切入点表达式作用:知道对哪个类里面的哪个方法进行增强

    (2)语法结构

    举例1:对com.atguigu.dao.BookDao类里面的add进行增强

    execution(* com.atguigu.dao.BookDao.add(…))

    举例2:对com.atguigu.dao.BookDao类里面的所有的方法进行增强

    execution(* com.atguigu.dao.BookDao.*(…))

    举例3:对com.atguigu.dao包里面所有类,类里面所有方法进行增强

    execution(* com.atguigu,dao..(…))

    AOP操作(AspectJ注解)

    1、创建类,在类里面定义方法

    public class User {
        public void add(){
            System.out.println("add....");
        }
    }
    
    2、创建增强类()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    AOP操作(AspectJ配置文件)

    (1) 在增强类里面,创建方法,让不同的方法代表不同类型

    //增强的类

    public class UserProxy {
        //前置通知
        public void before(){
            System.out.println("before....");
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    3、进行通知的配置

    (1)、在spring配置文件中,开启注解扫描

    
    
    
        
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    (2)、使用注解创建User和UserProxy对象

    增加@Component

    (3)、在增强类上面添加注解@Aspect

    (4)、在spring配置文件中开启生成代理对象

    
        
    
    • 1
    • 2

    4、配置不同对象通知

    package com.xxx.demo.AOP;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.*;
    import org.springframework.stereotype.Component;
    import sun.reflect.generics.tree.VoidDescriptor;
    
    //增强的类
    
    @Component
    @Aspect
    //生成代理对象
    public class UserProxy {
        //前置通知
        //@before注解表示作为前置通知
        @Before(value = "execution(* com.xxx.demo.AOP.User.add(..))")//切入点表达式
        public void before(){
            System.out.println("before....");
    
        }
        //后置通知
        @AfterReturning(value = "execution(* com.xxx.demo.AOP.User.add(..))")
    
        public void AfterReturning(){
            System.out.println("AfterReturning......");
        }
    
        //最终通知
        @After(value = "execution(* com.xxx.demo.AOP.User.add(..))")
    
        public void after(){
            System.out.println("after......");
        }
    
        //异常通知
        @AfterThrowing(value = "execution(* com.xxx.demo.AOP.User.add(..))")
    
        public void AfterThrowing(){
            System.out.println("AfterThrowing......");
        }
    
        //环绕通知
        @Around(value = "execution(* com.xxx.demo.AOP.User.add(..))")
    
        public void Around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
            System.out.println("环绕之前......");
            //被增强的方法执行
            ProceedingJoinPoint.proceed();
            System.out.println("环绕之后");
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    package com.xxx.demo.AOP;
    
    import org.springframework.stereotype.Component;
    
    //被增强的类
    @Component
    public class User {
        public void add(){
            System.out.println("add....");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    package com.xxx.demo.AOP;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class TestAop {
        @Test
        public void testAopAnno(){
            ApplicationContext context=new ClassPathXmlApplicationContext("bean5.xml");
            User user=context.getBean("user",User.class);
            user.add();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    
    
    
        
    
    
        
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    5、相同切入点抽取

    //相同切入点抽取
    
    @Pointcut(value = "execution(* com.xxx.demo.AOP.User.add())")
    public void PointcutDemo(){
        System.out.println("相同切入点抽取");   
    }
    
    6、有多个增强类同一个方法进行增强,设置增强类优先级
        (1)在增强类上面添加注解@Order(数字类型值) 数字类型越小优先级越高
        @Component
    	@Aspect
        @Order(1)
    public class PersonProxy {
        //设置多个增强类的优先级 写了注解才能调用execution函数
    
        @Before(value = "execution(* com.xxx.demo.AOP.User.add(..))")
        public void afterReturning(){
            System.out.println("Person Before");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    3、AOP操作(AspectJ配置文件)

    1、创建两个类,增强类和被增强类,创建方法

    public class Book {
        public void buy(){
            System.out.println("buy..........");
        }
    }
    
    public class BookProxy {
        public void before(){
            System.out.println("before.....");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2、在spring配置文件中创建两个类对象

    
        
        
    
    • 1
    • 2
    • 3

    3、在spring配置文件中配置切入点

    
        
    
            
    
    
            
    
                
            
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    4、 JdbcTemplate(概念和准备)

    1、什么是JdbcTemplate

    (1) 、Spring框架对JDBC进行封装,使用JdbcTemplate方便实现对数据库操作

    2、准备工作

    (1)、引入相关jar包

  • 相关阅读:
    Android MQTT:实现设备信息上报与远程控制
    【服务器数据恢复】RAID5强制上线后又被格式化的数据恢复案例
    uniApp笔记
    如何获取用户的ip地址
    花青素染料 Cy3NS 酸,Cy3NS acid,CAS:1032678-01-5物化性质解析
    图像质量评价指标
    【国漫逆袭】火灵儿重返第一巅峰,云曦排名飙升,不良人陷入颓势
    基于小程序制作一个猜拳小游戏
    Linux命令详解(13)- ln命令
    【ArcPy】批量读取文件夹excel中XY并转为点shp
  • 原文地址:https://blog.csdn.net/m0_62491934/article/details/126424289