【Spring AOP】静态代理设计模式、Spring 动态代理开发详解、切入点详解(切入点表达式、切入点函数

  1. 额外功能维护性差:在代理类中修改额外功能较为麻烦

Spring 动态代理开发

================================================================================

概念:通过代理类为原始类(目标类)增加额外功能

好处:利于原始类(目标类)的维护

搭建开发环境


org.springframework

spring-aop

5.1.14.RELEASE

org.aspectj

aspectjrt

1.8.9

org.aspectj

aspectjweaver

1.8.13

Spring 动态代理的开发步骤(5步)


  1. 创建原始对象(目标对象)

public interface UserService {

void register(User user);

boolean login(String name, String password);

}

public class UserServiceImpl implements UserService {

@Override

public void register(User user) {

System.out.println(“UserServiceImpl.register 业务运算 + DAO”);

}

@Override

public boolean login(String name, String password) {

System.out.println(“UserServiceImpl.login 业务运算 + DAO”);

return true;

}

}

  1. 额外功能 MethodBeforeAdvice 接口

public class Before implements MethodBeforeAdvice {

/**

  • 作用: 把需要运行在原始方法执行之前运行的额外功能, 书写在 before 方法中

*/

@Override

public void before(Method method, Object[] objects, Object o) throws Throwable {

System.out.println(“—method before advice log—”);

}

}

  1. 定义 切入点:额外功能的加入

目的: 由程序员根据自己的需要,决定额外功能加入给哪个原始方法(register、login)

aop:config

<aop:pointcut id=“pc” expression=“execution(* * (…))”/>

</aop:config>

  1. 组装(2、3 整合)

<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">

aop:config

<aop:pointcut id=“pc” expression=“execution(* * (…))”/>

<aop:advisor advice-ref=“before” pointcut-ref=“pc”/>

</aop:config>

  1. 调用

目的:获得 Spring 工厂创建的动态代理对象,并进行调用

注意:

  1. Spring 的工厂通过原始对象的 id 值获得的是代理对象

  2. 获得代理对象后,可以通过声明接口类型,进行对象的存储

/**

  • 用于测试动态代理

*/

@Test

public void test1() {

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(“/applicationContext.xml”);

UserService userService = (UserService) ctx.getBean(“userService”);

userService.login(“admin”, “1234”);

userService.register(new User());

}

动态代理细节分析


Spring 创建的动态代理类在哪里?

  • Spring 框架在运行时,通过动态字节码技术,在 JVM 创建的,运行在 JVM 内部,等程序结束后,会和 JVM 一起消失。

什么是 动态字节码技术

  • 通过第三方动态字节码框架,在 JVM 中创建对应类的字节码,进而创建对象,当虚拟机结束,动态字节码跟着消失。

结论:

  • 动态代理不需要定义类文件,都是 JVM 运行过程中动态创建的

所以不会造成静态代理的缺点:类⽂件数量过多,影响项目管理的问题。

在这里插入图片描述


动态代理编程简化代理的开发

  • 在额外功能不改变的前提下,创建其他目标类(原始类)的代理对象时,只需要指定原始(目标)对象即可。

动态代理使得 额外功能的维护性大大增强

动态代理开发详解

===========================================================================

额外功能的详解


MethodBeforeAdvice 分析

  1. MethodBeforeAdvice 接口作用:额外功能运行在原始方法执行之前,进行额外功能操作。

public class Before implements MethodBeforeAdvice {

/**

  • 作用: 把需要运行在原始方法执行之前运行的额外功能, 书写在 before 方法中

  • Method: 额外功能所增加给的那个原始方法

  •                      login
    
  •                      register
    
  •                      --------
    
  •                      showOrder
    
  • Object[]: 额外功能所增加给的那个原始方法的参数

  •                      String name,String password
    
  •                      User
    
  •                      --------
    
  • Object: 额外功能所增加给的那个原始对象

  •                      UserServiceImpl
    
  •                      ---------------
    
  •                      OrderServiceImpl
    

*/

@Override

public void before(Method method, Object[] objects, Object o) throws Throwable {

System.out.println(“—new method before advice log—”);

}

}

  1. before 方法的 3 个参数在实战中,该如何使用?

before 方法的参数,在实战中,会根据需要进行使用,不⼀定都会用到,也有可能都不用。

孙哥:”我用了 15 年 Spring 一次都没有用到过这个。"

MethodInterceptor(方法拦截器)

methodinterceptor 接口:额外功能可以根据需要运行在原始方法执行 前、后、前后

  • 参数:MethodInvocation:额外功能所增加给的那个原始方法 (login, register)

  • 返回值:Object:原始方法的返回值 (没有就返回 null)

  • invocation.proceed():原始方法运行

额外功能运行在原始方法 之前

public class Around implements MethodInterceptor {

@Override

public Object invoke(MethodInvocation methodInvocation) throws Throwable {

System.out.println(“—额外功能运行在原始方法执行之前—”);

Object ret = methodInvocation.proceed(); // 原始方法运行, 获取原始方法的返回值

return ret;

}

}

额外功能运行在原始方法 之后

public class Around implements MethodInterceptor {

@Override

public Object invoke(MethodInvocation methodInvocation) throws Throwable {

Object ret = methodInvocation.proceed(); // 原始方法运行, 获取原始方法的返回值

System.out.println(“—额外功能运行在原始方法执行之后—”);

return ret;

}

}

额外功能运行在原始方法 之前、之后

public class Around implements MethodInterceptor {

@Override

public Object invoke(MethodInvocation methodInvocation) throws Throwable {

System.out.println(“—额外功能运行在原始方法执行之前—”);

Object ret = methodInvocation.proceed(); // 原始方法运行, 获取原始方法的返回值

System.out.println(“—额外功能运行在原始方法执行之后—”);

return ret;

}

}

额外功能运行在原始方法抛出异常的时候:

public class Around implements MethodInterceptor {

@Override

public Object invoke(MethodInvocation methodInvocation) throws Throwable {

Object ret = null;

try {

ret = methodInvocation.proceed(); // 原始方法运行, 获取原始方法的返回值

} catch (Throwable throwable) {

System.out.println(“—额外功能运行在原始方法抛异常的时候—”);

}

return ret;

}

}

MethodInterceptor 影响原始方法的返回值:

public class Around implements MethodInterceptor {

@Override

public Object invoke(MethodInvocation methodInvocation) throws Throwable {

System.out.println(“—log—”);

Object ret = methodInvocation.proceed();

return false;

}

}

切入点详解

========================================================================

切入点决定额外功能加入位置(方法)

<aop:pointcut id=“pc” expression=“execution(* * (…))”/>

  • execution()切入点函数

  • * *(..)切入点表达式

切入点表达式


方法切入点

定义一个方法

public void add(int i, int j)

  •           *      (..) 
    
    • (…) --> 所有方法
  • —> 修饰符 返回值

  • —> 方法名

() —> 参数表

… —> 对于参数没有要求 (参数有没有,参数有⼏个都行,参数是什么类型的都行)

  • 定义 login 方法作为切入点:

<aop:pointcut id=“pc” expression=“execution(* login (…))”/>

<aop:pointcut id=“pc” expression=“execution(* register (…))”/>

  • 定义方法名为 login 且 有两个字符串类型的参数 作为切入点;

<aop:pointcut id=“pc” expression=“execution(* login (String,String))”/><

<aop:pointcut id=“pc” expression=“execution(* register (com.yusael.proxy.User))”/>

<aop:pointcut id=“pc” expression=“execution(* login(String, …))”/>

  • 精准方法切入点限定

修饰符 返回值 包 类.方法(参数)

<aop:pointcut id=“pc” expression=“execution(* com.yusael.proxy.UserServiceImpl.login(…))”/>

<aop:pointcut id=“pc” expression=“execution(* com.yusael.proxy.UserServiceImpl.login(String, String))”/>

类切入点

指定 特定类作为切入点(额外功能加入的位置),这个类中的所有方法,都会加上对应的额外功能。

  • 语法1

类中所有的方法加入了额外功能

<aop:pointcut id=“pc” expression=“execution(* com.yusael.proxy.UserServiceImpl.*(…))”/>

  • 语法2

忽略包

  1. 类只存在一级包

<aop:pointcut id=“pc” expression=“execution(* .UserServiceImpl.(…))”/>

  1. 类存在多级包

<aop:pointcut id=“pc” expression=“execution(* …UserServiceImpl.(…))”/>

包切入点(实战中用的多)

指定包作为额外功能加入的位置,自然包中的所有类及其方法都会加入额外的功能。

  • 语法1:

切入点包中的所有类,必须在proxy中,不能在proxy包的⼦包中

<aop:pointcut id=“pc” expression=“execution(* com.yusael.proxy..(…))”/>

  • 语法2:

切入点当前包及其⼦包都生效

<aop:pointcut id=“pc” expression=“execution(* com.yusael.proxy….(…))”/>

切入点函数(execution、args、within)


切入点函数:用于执行切入点表达式

exectuion

execution 是最为重要的切入点函数,功能最全;可以执行执行 方法切入点表达式类切入点表达式包切入点表达式

弊端:execution 执⾏切入点表达式 ,书写麻烦

execution(* com.yusael.proxy….(…))

注意:其他的 切入点函数 简化的是 execution 的书写复杂度,功能上完全⼀致。


args

args 作用:主要用于 函数(方法) 参数的匹配

切入点:方法参数必须得是 2 个字符串类型的参数

使用 execution

<aop:pointcut id=“pc” expression=“execution(* *(String, String))”/>

使用 args

<aop:pointcut id=“pc” expression=“args(String, String)”/>


within

within 作用:主要用于进行 类、包切入点表达式 的匹配。

切入点: UserServiceImpl 这个类

使用 execution

<aop:pointcut id=“pc” expression=“expression(* …UserServiceImpl.(…))”/>

使用 within

<aop:pointcut id=“pc” expression=“within(*…UserServiceImpl)”/>

切入点: com.yusael.proxy 这个包

使用 execution

<aop:pointcut id=“pc” expression=“execution(* com.yusael.proxy…*.**(…)”/>

最后

腾讯T3大牛总结的500页MySQL实战笔记意外爆火,P8看了直呼内行

腾讯T3大牛总结的500页MySQL实战笔记意外爆火,P8看了直呼内行

注意:其他的 切入点函数 简化的是 execution 的书写复杂度,功能上完全⼀致。


args

args 作用:主要用于 函数(方法) 参数的匹配

切入点:方法参数必须得是 2 个字符串类型的参数

使用 execution

<aop:pointcut id=“pc” expression=“execution(* *(String, String))”/>

使用 args

<aop:pointcut id=“pc” expression=“args(String, String)”/>


within

within 作用:主要用于进行 类、包切入点表达式 的匹配。

切入点: UserServiceImpl 这个类

使用 execution

<aop:pointcut id=“pc” expression=“expression(* …UserServiceImpl.(…))”/>

使用 within

<aop:pointcut id=“pc” expression=“within(*…UserServiceImpl)”/>

切入点: com.yusael.proxy 这个包

使用 execution

<aop:pointcut id=“pc” expression=“execution(* com.yusael.proxy…*.**(…)”/>

最后

[外链图片转存中…(img-pQ4s2IFm-1718783166637)]

[外链图片转存中…(img-H5WRQnqY-1718783166638)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值