Spring---Aop

AOP

什么是AOP

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

AOP(底层原理)

有两种情况动态代理
第一种有接口情况,使用jdk动态代理
创建接口实现类代理对象
在这里插入图片描述
第二种没有接口情况,使用CGIB动态代理
创建子类的代理对象,增强类的方法
在这里插入图片描述

2、编写 JDK 动态代理代码

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

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


}
public class UserDaoImpl implements  UserDao{
    @Override
    public int add(int a, int b) {
        return a+b;
    }

    @Override
    public String update(String id) {
        return id;
    }
}

```java
package com.atguigu.spring.Userdao;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

public class JDKPrixy {
    public static void main(String[] args) {
        //创建接口实现类代理对象
        Class[] interfaces = {UserDao.class};
        UserDaoImpl userDao = new UserDaoImpl();
        UserDao dao =
                (UserDao) Proxy.newProxyInstance(JDKPrixy.class.getClassLoader(), interfaces , new UserDaoProxy(userDao));
        int result = dao.add(1,2);
        System.out.println("result:"+result);
    }

}
//创建代理对象代码
class UserDaoProxy implements InvocationHandler{
    //1 把创建的是谁的代理对象,把谁传递过来
    //有参数构造传递
    private  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;
    }
}

AOP(术语)

1.连接点
类里面哪些方法可以增强,称为连接点
2.切入点
实际真正被增强的方法称为切入点
3.通知(增强)
具体增强的逻辑部分,称为通知
类型:前置通知,后置通知,环绕通知,异常通知,最终通知
4.切面
是动作,把通知应用到切入点的过程

操作(准备工作)

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

4.切入点表达式
(1)切入点表达式作用:知道对哪个类里面的哪个方法进行增强
(2)语法结构: execution([权限修饰符] [返回类型] [类全路径] 方法名称 )
举例 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("这是User的add方法");
    }

}

2、创建增强类(编写增强逻辑)

public class UserProxy {
  @Before(value = "execution(* com.atguigu.spring.aop.User.add(..))")
    public void before(){
        System.out.println("这是UserProxy的update方法。。。。" +
                "");
    }

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

      xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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.atguigu.spring.aop"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

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

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

@Component
@Aspect
public class UserProxy {
  @Before(value = "execution(* com.atguigu.spring.aop.User.add(..))")
    public void before(){
        System.out.println("这是UserProxy的update方法。。。。" +
                "");
    }

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

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

4、配置不同类型的通知
(1)在增强类的里面,在作为通知方法上面添加通知类型注解,使用切入点表达式配置`

@Component
@Aspect   // 生成代理对象
public class UserProxy {
// 前置通知
  @Before(value = "execution(* com.atguigu.spring.aop.User.add(..))")
    public void before(){
        System.out.println("这是UserProxy的update方法。。。。" +
                "");
    }
    
 // 后置通知(返回通知)
    @AfterReturning(value = "execution(* com.atguigu.spring.aop.User.add(..))")
    public void afterreturning(){
        System.out.println("这是方法AfterReturning。。。。" +
                "");
    }
 // 最终通知
    @After(value = "execution(* com.atguigu.spring.aop.User.add(..))")
    public void after(){
        System.out.println("这是方法After。。。。" +
                "");
    }
    //环绕通知
    @Around(value = "execution(* com.atguigu.spring.aop.User.add(..))")
    public void Around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕之前。。。。" +"");
      proceedingJoinPoint.proceed();
        System.out.println("环绕之后。。。。" +
                "");
    }

// 异常通知
    @AfterThrowing(value = "execution(* com.atguigu.spring.aop.User.add(..))")
    public void AfterThrowin(){
        System.out.println("这是方法Around。。。。" +
                "");
    }

}

效果展示
在这里插入图片描述
运行正常不会执行Afterthrow方法,如果发生异常,会执行且不执行AfterReturning方法

5、相同的切入点抽取

  @Pointcut(value ="execution(* com.atguigu.spring.aop.User.add(..))" )
    public void pointcut(){

    }
  @Before(value = "pointcut()")
    public void before(){
        System.out.println("这是UserProxy的update方法。。。。" +
                "");
    }

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

@Component
@Aspect
@Order(1)
public class PersonProxy {
    @Before(value = "execution(* com.atguigu.spring.aop.User.add(..))")
    public void afterreturning(){
        System.out.println("这是方法person。。。。" +
                "");
    }
}

在这里插入图片描述
7、完全使用注解开发 (1)创建配置类,不需要创建 xml 配置文件

@Configuration
@ComponentScan(basePackages = {"com.atguigu"})
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class ConfigAop {

}

@Configuration
@ComponentScan(basePackages = {“com.atguigu”})
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class ConfigAop {

}

AOP操作(AspectJ)

创建两个类,增强类和被增强类,创建方法
在spring配置文件中创建两个类对象

<bean id="boo" class="com.atguigu.spring.aoppro.Boo"></bean>
    <bean id="booproxy" class="com.atguigu.spring.aoppro.BooProxy"></bean>
    

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

<!--配置aop增强-->
    <aop:config>
      <!--  切入点-->
        <aop:pointcut id="p" expression="execution(* com.atguigu.spring.aoppro.Boo.add(..))"/>
       <!-- 配置切面-->
        <aop:aspect ref="booproxy">
            <!--增强到具体方法-->
            <aop:before method="before" pointcut-ref="p"></aop:before>
        </aop:aspect>
    </aop:config>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值