Spring中AOP概念与两种 * 模式原理详解

作者:HouFei-Liu 时间:2023-04-13 01:21:54 

1.概念

1.AOP技术简介

AOP 为Aspect Oriented Programming 的缩写,意思为面向切面编程,是通过预编译方式和运行期 * 实现程序功能的统一维护的一种技术。

AOP 是 OOP 的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

2.AOP的优势

作用:在程序运行期间,在不修改源码的情况下对方法进行功能增强优势:减少重复代码,提高开发效率,并且便于维护

3.Spring AOP术语

Spring 的 AOP 实现底层就是对上面的 * 的代码进行了封装,封装后我们只需要对需要关注的部分进行代码编写,并通过配置的方式完成指定目标的方法增强。在正式讲解 AOP 的操作之前,我们必须理解 AOP 的相关术语,常用的术语如下:

Target(目标对象):代理的目标对象Proxy (代理):一个类被 AOP 织入增强后,就产生一个结果代理类Joinpoint(连接点):所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点.(可能被增强的方法)Pointcut(切入点):所谓切入点是指我们要对哪些 Joinpoint 进行拦截的定义(被增强的方法)Advice(通知/ 增强):所谓通知是指拦截到 Joinpoint 之后所要做的事情就是通知(对目标对象增强的方法)Aspect(切面):是切入点和通知(引介)的结合(目标方法+增强=切面)Weaving(织入):是指把增强应用到目标对象来创建新的代理对象的过程。 spring采用 * 织入,而AspectJ采用编译期织入和类装载期织入.(一个动作,切点和通知结合的过程=织入)

4.AOP 开发明确的事项

需要编写的内容 编写核心业务代码(目标类的目标方法)编写切面类,切面类中有通知(增强功能方法)在配置文件中,配置织入关系,即将哪些通知与哪些连接点进行结合 AOP 技术实现的内容 Spring 框架监控切入点方法的执行。一旦监控到切入点方法被运行,使用代理机制,动态创建目标对象的代理对象,根据通知类别,在代理对象的对应位置,将通知对应的功能织入,完成完整的代码逻辑运行。 AOP 底层使用哪种代理方式 在 spring 中,框架会根据目标类是否实现了接口来决定采用哪种 * 的方式。

 2.AOP底层实现

实际上, AOP 的底层是通过 Spring 提供的的 * 技术实现的。在运行期间, Spring通过 * 技术动态的生成代理对象,代理对象方法执行时进行增强功能的介入,在去调用目标对象的方法,从而完成功能的增强。

1.AOP 的 * 技术:

常用的 * 技术

  • JDK 代理 : 基于接口的 * 技术

  • cglib 代理:基于父类的 * 技术

Spring中AOP概念与两种 * 模式原理详解

2.基于jdk的 * 代码


//---------接口1------------
package com.itspring.proxy.jdk;

public interface TargetInterface1 {
   void save();
}

//---------接口2------------
package com.itspring.proxy.jdk;

public interface TargetInterface2 {
   void update();
}

//---------接口1,接口2实现类(目标类)------------
package com.itspring.proxy.jdk;

//目标类(被增强的类)
public class Target implements TargetInterface1 ,TargetInterface2{
   public void save() {
       System.out.println("save running...");
   }

public void update() {
       System.out.println("update running...");
   }
}


//---------通知类(方法增强类)------------
package com.itspring.proxy.jdk;

//通知类(增强类)
public class Advice {

public void before() {
       System.out.println("前置增强...");
   }

public void afterRunning() {
       System.out.println("后置增强...");
   }
}


//---------测试代码------------
package com.itspring.proxy.jdk;

import org.junit.Test;

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

public class ProxyTest {

@Test
   public void test1() {

final Target target = new Target();

final Advice advice = new Advice();

//返回值就是动态生成的代理对象
       TargetInterface1 proxy1 = (TargetInterface1) Proxy.newProxyInstance(
               target.getClass().getClassLoader(),  //目标类的类加载器
               target.getClass().getInterfaces(),  //目标类实现的接口(可能有多个)
               new InvocationHandler() {
                   //调用代理对象的任何方法,实质上都是调用invoke方法
                   public Object invoke(Object proxy,  //代理对象
                                        Method method,  //目标方法对象
                                        Object[] args  //目标方法的参数
                   ) throws Throwable {
                       System.out.println("正在执行的方法:" + method.getName());
                       advice.before();  //前置增强
                       Object invoke = method.invoke(target, args);  //执行目标方法
                       advice.afterRunning();  //后置增强
                       return invoke;
                   }
               }
       );

proxy1.save();

//返回值就是动态生成的代理对象
       TargetInterface2 proxy2 = (TargetInterface2) Proxy.newProxyInstance(
               target.getClass().getClassLoader(),  //目标类的类加载器
               target.getClass().getInterfaces(),  //目标类实现的接口(可能有多个)
               new InvocationHandler() {
                   //调用代理对象的任何方法,实质上都是调用invoke方法
                   public Object invoke(Object proxy,  //代理对象
                                        Method method,  //目标方法对象
                                        Object[] args  //目标方法的参数
                   ) throws Throwable {
                       System.out.println("正在执行的方法:" + method.getName());
                       advice.before();
                       Object invoke = method.invoke(target, args);  //执行目标方法
                       advice.afterRunning();
                       return invoke;
                   }
               }
       );

proxy2.update();
   }
}

Spring中AOP概念与两种 * 模式原理详解

3.基于cglib的 * 代码

cglib是第三方的库,spring集成了cglib.

Spring中AOP概念与两种 * 模式原理详解

Spring中AOP概念与两种 * 模式原理详解


//---------目标类-------------
package com.itspring.proxy.cglib;

import com.itspring.proxy.jdk.TargetInterface1;
import com.itspring.proxy.jdk.TargetInterface2;

//目标类(被增强的类)
public class Target {
   public void save() {
       System.out.println("save running...");
   }

public void update() {
       System.out.println("update running...");
   }
}


//---------通知类(增强类)-------------
package com.itspring.proxy.cglib;

//通知类(增强类)
public class Advice {

public void before() {
       System.out.println("前置增强...");
   }

public void afterRunning() {
       System.out.println("后置增强...");
   }
}


//---------测试代码-------------
package com.itspring.proxy.cglib;

import com.itspring.proxy.jdk.TargetInterface1;
import com.itspring.proxy.jdk.TargetInterface2;
import org.junit.Test;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

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

public class ProxyTest {

@Test
   public void test1() {
       //目标对象
       final Target target = new Target();

//增强对象
       final Advice advice = new Advice();

//基于cglib生成 * 对象
       //1.创建增强器
       Enhancer enhancer = new Enhancer();
       //2.创建父类
       enhancer.setSuperclass(Target.class);
       //3.设置回调
       enhancer.setCallback(new MethodInterceptor() {
           public Object intercept(Object proxy,  //代理对象
                                   Method method,  //目标方法
                                   Object[] objects,  //目标方法的参数
                                   MethodProxy methodProxy)  //目标方法的代理
                   throws Throwable {
               //前置增强
               advice.before();
               //目标方法
               Object invoke = method.invoke(target, objects);
               //后置增强
               advice.afterRunning();
               return invoke;
           }
       });
       //4.生成代理对象
       Target target1 = (Target) enhancer.create();
       //5.测试
       target1.save();
       target1.update();

}
}

Spring中AOP概念与两种 * 模式原理详解

来源:https://blog.csdn.net/qq_36109528/article/details/120884879

标签:spring, , ,aop
0
投稿

猜你喜欢

  • android手机获取唯一标识的方法

    2022-05-28 19:24:38
  • C# 实现dataGridView选中一行右键出现菜单的示例代码

    2022-05-12 05:15:55
  • C#特性 扩展方法

    2023-01-15 03:45:15
  • Android使用分类型RecyclerView仿各大商城首页

    2021-06-29 12:08:23
  • java实现飞机游戏代码

    2022-01-08 06:48:34
  • android底层去掉虚拟按键的实例讲解

    2022-01-29 17:53:14
  • Android开发中使用外部应用获取SD卡状态的方法

    2023-02-01 21:03:45
  • 使用itextpdf解决PDF合并的问题

    2023-09-21 04:47:16
  • java8 stream自定义分组求和并排序的实现

    2022-09-12 04:08:26
  • 基于Aforge摄像头调用简单实例

    2022-07-23 03:44:20
  • Java设计模式之观察者模式(Observer模式)介绍

    2022-10-16 04:40:42
  • Spring IOC与DI核心重点分析

    2023-11-12 14:35:55
  • Java多线程编程中ThreadLocal类的用法及深入

    2022-03-17 03:21:29
  • SpringBoot2 Jpa 批量删除功能的实现

    2023-06-18 04:54:35
  • c#使用xamarin编写拨打电话程序

    2023-09-04 18:09:20
  • java 字浮串提取方法汇集

    2023-11-24 14:43:16
  • Android基础之隐藏标题栏/设置为全屏/横竖屏切换

    2022-06-22 14:29:35
  • java并发之ArrayBlockingQueue详细介绍

    2023-04-23 07:30:01
  • SpringMVC @RequestMapping注解详解

    2022-08-08 06:58:14
  • 简单了解java自定义和自然排序

    2022-01-29 02:47:26
  • asp之家 软件编程 m.aspxhome.com