Spring-AOP(面向切面编程)

标题AOP概念

1、什么是AOP?
(1)面向切面编程,利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑之间的各部分耦合度降低,提高程序的可重用性,同时提高了开发的效率。
(2)通俗解释:不通过修改源代码的方式,在主干功能里添加新功能

AOP底层原理
1、AOP底层使用代理
(1)有两种情况的动态代理
第一种,有接口情况,使用JDK动态代理
创建接口实现类对象,增强类的方法
在这里插入图片描述
UserDao

public interface UserDao {
    public int add(int a,int b);
    public String update(String id);
}

UserDaoImpl

public class UserDaoImpl implements UserDao{
    @Override
    public int add(int a, int b) {
        System.out.println("add执行了");
        return a+b;
    }

    @Override
    public String update(String id) {
        System.out.println("update执行了");
        return id;
    }
}

JDKProxy

public class JDKProxy {
    //创建接口实现类代理对象
    public static void main(String[] args) {
        //代理接口而不是实现类
        Class[] interfaces={UserDao.class};
        //newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)
        //          返回一个指定接口的代理类实例,该接口可以将方法调用指派到指定的调用处理程序。
        //     loader - 类加载器来定义代理类
        //    interfaces - 代理类实现的接口列表
        //    h - 调度方法调用的调用处理函数
        UserDaoImpl userDao=new UserDaoImpl();
        UserDao dao=(UserDao) Proxy.newProxyInstance(JDKProxy.class.getClassLoader(),interfaces,new UserDaoProxy(userDao));
        int result=dao.add(2,3) ;
        System.out.println(result);
    }

}
//创建代理对象代码
class UserDaoProxy implements InvocationHandler{
    //创建的谁的代理对象,就把谁传进来
    public Object obj;
    public UserDaoProxy(Object obj){
        this.obj=obj;
    }
//增强的逻辑
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //执行方法之前
        System.out.println("方法的名字:"+method.getName()+"参数:"+ Arrays.toString(args));

        //执行方法
        Object res=method.invoke(obj,args);

        //执行方法之后
        System.out.println(obj);

        return res;
    }
}

第二种,没有接口情况,使用CGLIB进行动态代理
创建子类代理对象,增强类的方法

在这里插入图片描述

标题AOP术语

1、连接点
类里面可以被增强的方法
2、切入点
实际被真正增强的方法
3、通知(增强)
(1)实际增强的逻辑部分
(2)通知有多种类型

  • 前置通知
  • 后置通知
  • 环绕通知
  • 异常通知
  • 最终通知

4、切面(是动作)
把通知切入到点的过程

AOP操作(准备)

1、spring框架一般都是基于AspectJ实现AOP操作
AspectJ不是Spring组成部分,独立AOP框架,一般AspectJ和Spring框架一起使用,进行AOP操作
2、基于Aspectj实现AOP操作
方法一:基于xml配置文件实现
方法二:基于注解方式实现(一般使用此方法)
3、在项目工程中引入AOP相关依赖
在这里插入图片描述

4、切入点表达式
(1)切入点表达式的作用:知道对哪个类里面的那个方法进行增强
(2)语法结构

execution([权限修饰符][返回类型][类全路径]方法名称)
注:权限修饰符可以省略
举例1:对com.gongda.dao.BookDao里面的add方法进行增强
execution(* com.gongda.dao.BookDao.add(…))
举例2:对com.gongda.dao.BookDao里面的所有方法进行增强
execution(* com.gongda.dao.BookDao.(…))
举例2:对com.gongda.dao包里面的所有类,类里面所有方法进行增强
execution(
com.gongda.dao..(…))

AOP操作(AspectJ配置文件)

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

public class User {
    public void add(){
        System.out.println("执行add方法。。。。");
    }
}

2、创建增强类,编写增强方法

public class UserProxy {
    //前置通知
    @Before(value="execution(* com.gongda.Spring5.aop.User.add(..))")
    public void Before(){
        System.out.println("before...");
    }

    //后置通知
    @AfterReturning(value="execution(* com.gongda.Spring5.aop.User.add(..))")
    public void AfterReturning(){
        System.out.println("afterReturning...");
    }

    //环绕通知
    @Around(value="execution(* com.gongda.Spring5.aop.User.add(..))")
    **public void Around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕前...");**

        //执行被增强的方法
        proceedingJoinPoint.proceed();

        System.out.println("环绕后...");
    }

    //错误通知
    @AfterThrowing(value="execution(* com.gongda.Spring5.aop.User.add(..))")
    public void AfterThrowing(){
    }

    //最终通知
    @After(value = "execution(* com.gongda.Spring5.aop.User.add(..))")
    public void After(){
        System.out.println("after...");
    }

}

3、进行通知配置
(1)在spring文件中,实现注解扫描

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启注解扫描-->
    <context:component-scan base-package="com.gongda.Spring5.aop" ></context:component-scan>
   
</beans>

(2)使用注解创建User和UserProxy对象
在这里插入图片描述
在这里插入图片描述

(3)在增强类上面添加注解@Aspect
在这里插入图片描述

(4)在Spring文件中开启生成代理对象

 <!--开启Aspect生成代理对象(扫描类上面的@Aspect,然后创建代理对象)-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

4、公共切入点抽取

  //相同切入点抽取
  //方便修改路径,开发中经常用到
    @Pointcut(value="execution(* com.gongda.Spring5.aop.User.add(..))")
    public void pointdemo(){

    }

    //前置通知
    @Before(value="pointdemo()")
    public void Before(){
        System.out.println("before...");
    }

5、有多个增强类多同一个方法进行增强,设置增强类优先级
(1)在增强类上面添加注解@Order(数字类型值),数字类型值越小优先级越高

6、使用完全注解开发 @Configuration @ComponentScan(basePackages={“com.gongda.Spring5”})//开启组件扫描 @EnableAspectJAutoProxy(proxyTargetClass=true)//开启Aspect生成代理对象 ## AOP 操作(AspectJ 配置文件)
<!--1、创建两个类,增强类和被增强类,创建方法(同上一样)-->
<!--2、在 spring 配置文件中创建两个类对象-->
<!--创建对象-->
<bean id="book" class="com.atguigu.spring5.aopxml.Book"></bean>
<bean id="bookProxy" class="com.atguigu.spring5.aopxml.BookProxy"></bean>
<!--3、在 spring 配置文件中配置切入点-->
<!--配置 aop 增强-->
<aop:config>
 <!--切入点-->
 <aop:pointcut id="p" expression="execution(* com.atguigu.spring5.aopxml.Book.buy(..))"/>
 <!--配置切面-->
 <aop:aspect ref="bookProxy">
 <!--增强作用在具体的方法上-->
 <aop:before method="before" pointcut-ref="p"/>
 </aop:aspect>
</aop:config>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值