30no2's Blog.

Spring黑马学习笔记五

字数统计: 1.8k阅读时长: 8 min
2021/04/28 Share

打卡第五天。加油。。。。

Spring的AOP

AOP的意思是面向切面编程,是通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。

AOP是OOP的延续,是函数式编程的一种衍生泛型,利用AOP可以对业务逻辑的各个部分进行隔离,从而使业务逻辑各部分之间的耦合度降低,提高程序的可用性,同时提高了开发的效率。

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

1、AOP的底层实现

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

2、AOP的动态代理技术

常用的动态代理技术

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    //Advice
    package com.wwq.proxy.jdk;

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

    public void afterReturning(){
    System.out.println("后置增强。。。。。。");
    }
    }
    //ProxyTest
    package com.wwq.proxy.jdk;

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

    public class ProxyTest {
    public static void main(String[] args) {
    //目标对象
    final Target target = new Target();
    //增强对象
    final Advice advice = new Advice();

    //返回值就是动态生成的代理对象
    TargetInterface proxy = (TargetInterface) Proxy.newProxyInstance(
    target.getClass().getClassLoader(),//目标对象类加载器
    target.getClass().getInterfaces(),//目标对象相同的接口字节码对象数组
    new InvocationHandler() {
    //去调用代理对象的任何方法,实际执行的都是invoke方法
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    //前置增强
    advice.before();
    //执行目标方法
    method.invoke(target, args);//执行目标方法
    //后置增强
    advice.afterReturning();
    return null;
    }
    }
    );
    //调用代理对象的方法
    proxy.save();
    }
    }
    //Target
    package com.wwq.proxy.jdk;

    public class Target implements TargetInterface {
    public void save() {
    System.out.println("save running.....");
    }
    }
    //TargetInterface
    package com.wwq.proxy.jdk;

    public interface TargetInterface {
    public void save();

    }
  • cglib 代理:基于父类的动态代理技术

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    //Advice
    package com.wwq.proxy.cglib;

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

    public void afterReturning(){
    System.out.println("后置增强。。。。。。");
    }
    }
    //ProxyTest
    package com.wwq.proxy.cglib;

    import com.wwq.proxy.jdk.TargetInterface;
    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 {
    public static void main(String[] args) {
    //目标对象
    final Target target = new Target();
    //增强对象
    final Advice advice = new Advice();

    //返回值就是动态生成的代理对象,基于cglib
    //1、 创建增强器
    final Enhancer enhancer = new Enhancer();
    //2、 设置父类(目标)
    enhancer.setSuperclass(Target.class);
    //3、 设置回调
    enhancer.setCallback(new MethodInterceptor() {
    public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
    //执行前置
    advice.before();
    //执行目标
    Object invoke = method.invoke(target, args);
    //执行后置
    advice.afterReturning();

    return invoke;
    }
    });
    //4、 创建代理对象
    Target target1 = (Target) enhancer.create();
    target1.save();
    }
    }
    //Target
    package com.wwq.proxy.cglib;

    public class Target{
    public void save() {
    System.out.println("save running.....");
    }
    }

下面是4月29号学习的。昨天啥也没干。打卡第六天

3、基于XML的AOP开发

3.1快速入门

  • 导入AOP相关坐标
  • 创建目标接口和目标类(内部有切点)
  • 创建切面类(内部有增强方法)
  • 将目标类和切面类的对象创建权交给spring
  • 在applicationController.xml中配置织入关系
  • 测试代码

今天是2021年5月10日,中间间断了一些时间。复习之前的一些知识。现在开始,继续学习。打卡第七天

applicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">

<!-- 目标对象-->
<bean id="target" class="com.wwq.aop.Target"/>
<!-- 切面对象-->
<bean id="myAspect" class="com.wwq.aop.MyAspect"/>
<!-- 配置植入,告诉spring框架,哪些方法(切点)-->
<aop:config>
<!-- 声明切面-->
<!---->
<aop:aspect ref="myAspect">
<!-- 切面,切点,通知-->
<aop:before method="before" pointcut="execution(public void com.wwq.aop.Target.save())" />
<aop:after method="after" pointcut="execution(public void com.wwq.aop.Target.save())" />
</aop:aspect>
</aop:config>
<context:component-scan base-package="com.wwq"/>

</beans>

MyAspect.java

1
2
3
4
5
6
7
8
9
10
package com.wwq.aop;

public class MyAspect {
public void before(){
System.out.println("前置增强。。。。。。");
}
public void after(){
System.out.println("后置增强。。。。");
}
}

3.2 XML配置AOP详解

1、切点表达式的写法

表达式语法:

execution([修饰符]返回值类型 报名,类名,方法名(参数))

  • 访问修饰符可以省略

  • 返回值类型,包名,类名,方法名可以使用星号*代表任意

  • 包名与类名之间的一个点,代表当前包下的类,两个点表示当前包及其子包下的类

  • 参数列表可以使用两个点..表示任意个数,任意类型的参数列表

    例如

    1
    2
    exection(* com.itheima.aop.*.*(..))
    exection(* *..*.*(..))

2、增强的一些配置

1
2
3
4
5
6
7
8
9
10
11
12
       <aop:aspect ref="myAspect">
<!-- 切面,切点,通知-->
<!-- <aop:before method="before" pointcut="execution(public void com.wwq.aop.Target.save())" />-->
<!-- 前置增强-->
<aop:before method="before" pointcut="execution(* com.wwq.aop.*.*(..))" />
<!-- 后置增强-->
<aop:after method="after" pointcut="execution(public void com.wwq.aop.Target.save())" />
<!-- 环绕-->
<aop:around method="around" pointcut="execution(* com.wwq.aop.*.*(..))"/>
<!-- 异常-->
<aop:after-throwing method="afterThrowing" pointcut="execution(* com.wwq.aop.*.*(..))"/>
</aop:aspect>

3、通知的配置语法

1
<aop:通知类型 method="切面类中方法名" pointcut="切点表达式"></aop:通知类型>
名称 标签 说明
前置通知 < aop:before > 用于配置前置通知。指定增强的方法在切入点方法之前执行。
后置通知 < aop:after-returning > 用于配置环绕通知。指定增强的方法在切入点方法之后执行。
环绕通知 < aop:around > 用于配置环绕通知。指定增强的方法在切入点方法之前和之后执行
异常抛出通知 < aop:throwing > 用于配置一场抛出通知。执行增强的方法在出现异常的时候执行
最终通知 < aop:after > 用于配置最终通知。无论增强的方式执行是否有异常都会执行

3.3 基于注解的AOP开发

1、注解的配置语法

1
@通知注解("切点表达式")//通知的配置语法
名称 标签 说明
前置通知 @Before 用于配置前置通知。指定增强的方法在切入点方法之前执行。
后置通知 @AfterReturning 用于配置环绕通知。指定增强的方法在切入点方法之后执行。
环绕通知 @Around 用于配置环绕通知。指定增强的方法在切入点方法之前和之后执行
异常抛出通知 @AfterThrowing 用于配置一场抛出通知。执行增强的方法在出现异常的时候执行
最终通知 @After 用于配置最终通知。无论增强的方式执行是否有异常都会执行
CATALOG
  1. 1. Spring的AOP
    1. 1.1. 1、AOP的底层实现
    2. 1.2. 2、AOP的动态代理技术
    3. 1.3. 3、基于XML的AOP开发
      1. 1.3.1. 3.1快速入门
      2. 1.3.2. 3.2 XML配置AOP详解
      3. 1.3.3. 3.3 基于注解的AOP开发