010Spring之AOP

本文详细讲解了AOP的概念,包括面向切面编程的原理,如何使用JDK和CGLIB动态代理实现AOP,以及AspectJ的注解和XML配置方式。通过实例演示,展示了如何在Spring框架中配置通知和切点,提升代码复用和灵活性。
摘要由CSDN通过智能技术生成

1、AOP相关概念

1、什么是AOP

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

2、AOP底层原理

1、AOP底层使用动态代理

有两种情况动态代理

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

⚫创建接口实现类代理对象,增强类的方法
在这里插入图片描述
使用Proxy类里面的方法创建代理对象
在这里插入图片描述
调用newProxyInstance方法
在这里插入图片描述

方法有三个参数:
第一参数,类加载器
第二参数,增强方法所在的类,这个类实现的接口,支持多个接口
第三参数,实现这个接口InvocationHandler,创建代理对象,写增强的部分
环境搭建
复制demo4重命名为demo5,删除原有的包,保留项目结构如下:
在这里插入图片描述
(1)创建接口,定义方法

package com.francis.spring5.mapper;

/**
 * @author Francis
 * @create 2021-06-07-21:36
 */
public interface UserMapper {
    public int add(int a,int b);
    public String update(String name);
}

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

package com.francis.spring5.mapper;

/**
 * @author Francis
 * @create 2021-06-07-21:37
 */
public class UserMapperImpl implements UserMapper{
    @Override
    public int add(int a, int b) {
        return a+b;
    }

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

(3)创建代理类

package com.francis.spring5.mapper;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author Francis
 * @create 2021-06-07-21:42
 */
public class UserMapperProxy implements InvocationHandler {
    /**1、把需要创建的代理对象传入
     * 有参构造器传入
     */
   private Object object;

    public UserMapperProxy(Object object) {
        this.object = object;
    }

    //增强的逻辑
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //方法之前执行
        System.out.println("增强的方法之前执行"+method.getName()+"传递的参数:"+args);

        //被增强的方法执行
        Object result = method.invoke(object, args);

        //方法之后执行
        System.out.println("方法之后执行"+object);
        return result;
    }
}

(4)使用Proxy类创建接口代理对象

package com.francis.spring5.mapper;

import org.junit.Test;

import java.lang.reflect.Proxy;

/**
 * @author Francis
 * @create 2021-06-07-21:45
 */
public class JDKProxyTest {

    @Test
    public void jdkProxyTest(){
        Class [] interfaces={UserMapper.class};
        UserMapperImpl userMapper = new UserMapperImpl();

        UserMapper mapper =(UserMapper) Proxy.newProxyInstance(JDKProxyTest.class.getClassLoader(),
                interfaces,new UserMapperProxy(userMapper) );
        int result = mapper.add(1, 2);
        System.out.println(result);
    }
}

在这里插入图片描述

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

⚫创建子类的代理对象,增强类的方法
在这里插入图片描述

3、AOP术语

1、连接点

类里面可以被增强的方法,叫做连接点

2、切入点

真正被增强的方法叫做切入点

3、通知
1、通知的含义

实际增强的逻辑部分成为通知,我们也成为增强

2、通知有多种类型

(1)前置通知
在切入点之前执行
(2)后置通知
在切入点之后执行
(3)环绕通知
在切入点前后都执行
(4)异常通知
发生异常时执行,类似于try catch里的catch部分
(5)最终通知
无论如何最终都执行,类似于try catch finally里的finally部分。

4、切面

是一个动作,把通知应用到切入点的过程(即通知与切入点绑定的过程)

4、环境准备

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

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

2、基于AspectJ实现AOP操作

(1)基于xml配置文件实现
(2)基于注解方式实现(推荐使用)

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

在这里插入图片描述
首先了解一下切入点表达式
(1)切入点表达式的作用:指定哪个类的哪个方法被增强
(2)语法结构:
execution([权限修饰符] [返回类型] [类全路径] [方法名称] ([参数列表]) )
在这里插入图片描述

5、使用AspectJ实现AOP

1、基于注解方式实现(推荐使用)
1、创建被增强的类
package com.francis.spring5.annotation_aop;

import org.springframework.stereotype.Component;

/**
 * @author Francis
 * @create 2021-06-08-11:06
 * 被增强的类
 */
@Component
public class User {
    public void add(){
        System.out.println
                ("user`s add");
    }
}
2、创建增强的类
package com.francis.spring5.annotation_aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @author Francis
 * @create 2021-06-08-11:07
 * 增强的类
 * @Aspect 标志位增强类
 */
@Component
@Aspect
public class UserProxy {
    //配置为前置通知
    @Before(value = "execution(* com.francis.spring5.annotation_aop.User.add(..))")
    public void  before(){
        System.out.println
                ("before method");
    }
    //配置为最终通知,在方法执行之后执行,无论是否发生异常都会执行
    @After(value = "execution(* com.francis.spring5.annotation_aop.User.add(..))")
    public void  after(){
        System.out.println
                ("after method");
    }
    //配置为后置通知,在方法返回结果之后执行,发生异常就不会执行
    @AfterReturning(value = "execution(* com.francis.spring5.annotation_aop.User.add(..))")
    public void  afterReturning(){
        System.out.println
                ("afterReturning method");
    }
    //配置为异常通知
    @AfterThrowing(value = "execution(* com.francis.spring5.annotation_aop.User.add(..))")
    public void  afterThrowing(){
        System.out.println
                ("afterThrowing method");
    }
    //配置为环绕通知
    @Around(value = "execution(* com.francis.spring5.annotation_aop.User.add(..))")
    public void  around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println
                ("环绕之前");
        proceedingJoinPoint.proceed();
        System.out.println
                ("环绕之后");

    }

}
3、进行通知的配置

(1)在spring的配置文件中,开启注解扫描
(2)使用注解创建User和UserProxy对象
(3)在增强的类上面添加注解@Aspect
(4)在配置文件中开启生成代理对象

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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.francis.spring5.annotation_aop"/>
<!--    开启生成代理对象-->
    <aop:aspectj-autoproxy/>

</beans>

测试没有异常的情况
在这里插入图片描述
测试有异常的情况
在这里插入图片描述
细节加强
(1)重用切入点
上面的例子中,每个通知的切入点表达式都是一样了,为了减少代码冗余,我们可以通过配置公共切入点。
在这里插入图片描述

package com.francis.spring5.annotation_aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @author Francis
 * @create 2021-06-08-11:07
 * 增强的类
 * @Aspect 标志位增强类
 */
@Component
@Aspect
public class UserProxy {
    /**
     * 定义公共切入点
     */
    @Pointcut(value = "execution(* com.francis.spring5.annotation_aop.User.add(..))")
    public void pointcutDemo(){

    }
    //配置为前置通知
    @Before(value = "pointcutDemo()")
    public void  before(){
        System.out.println
                ("before method");
    }
    //配置为最终通知,在方法执行之后执行,无论是否发生异常都会执行
    @After(value = "execution(* com.francis.spring5.annotation_aop.User.add(..))")
    public void  after(){
        System.out.println
                ("after method");
    }
    //配置为后置通知,在方法返回结果之后执行,发生异常就不会执行
    @AfterReturning(value = "pointcutDemo()")
    public void  afterReturning(){
        System.out.println
                ("afterReturning method");
    }
    //配置为异常通知
    @AfterThrowing(value = "pointcutDemo()")
    public void  afterThrowing(){
        System.out.println
                ("afterThrowing method");
    }
    //配置为环绕通知
    @Around(value = "pointcutDemo())")
    public void  around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println
                ("环绕之前");
        proceedingJoinPoint.proceed();
        System.out.println
                ("环绕之后");

    }

}

(2)多个增强类对同一个方法进行增强,设置通知的优先级
在这里插入图片描述

package com.francis.spring5.annotation_aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author Francis
 * @create 2021-06-08-13:29
 * @Order(1) 数字越小,优先级越高
 */
@Aspect
@Component
@Order(1)
public class PersonProxy {
    @Before(value = "execution(* com.francis.spring5.annotation_aop.User.add(..))")
    public void person(){
        System.out.println("这是person的before增强");
    }
}
2、基于xml配置文件实现
1、创建增强类及被增强类
package com.francis.spring5.xml_aop;

/**
 * @author Francis
 * @create 2021-06-08-13:40
 */
public class Book {
    public void buy(){
        System.out.println("book的buy方法");
    }
}
package com.francis.spring5.xml_aop;

/**
 * @author Francis
 * @create 2021-06-08-13:42
 */
public class BookProxy {
    public void before(){
        System.out.println("代理类的before增强方法");
    }
}
2、在配置文件中创建对象并配置切面
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
    <bean id="book" class="com.francis.spring5.xml_aop.Book"/>
    <bean id="bookProxy" class="com.francis.spring5.xml_aop.BookProxy"/>
    <!--配置aop增强-->
    <aop:config>
        <!--配置切入点-->
        <aop:pointcut id="buyPointcut" expression=
        "execution(* com.francis.spring5.xml_aop.Book.buy(..))"/>
        <!--配置切面-->
        <aop:aspect ref="bookProxy">
            <!--增强作用在具体方法上-->
            <aop:before method="before" pointcut-ref="buyPointcut"/>
         </aop:aspect>

    </aop:config>
</beans>
  @Test
    public void annoTest1(){
        ApplicationContext context =
new ClassPathXmlApplicationContext("bean2.xml");
        Book book = context.getBean("book", Book.class);
        book.buy();
    }

在这里插入图片描述
完全注解开发
用配置类来代替配置文件,这里直接给出配置类
在这里插入图片描述

package com.francis.spring5.common.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
 * @author Francis
 * @create 2021-06-08-13:59
 */
@Configuration
@ComponentScan(basePackages = {"com.francis.spring5.annotation_aop"})
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class AopConfig {
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值