zxpnet网站 zxpnet网站
首页
前端
后端服务器
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

zxpnet

一个爱学习的java开发攻城狮
首页
前端
后端服务器
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 大后端课程视频归档
  • 南航面试题
  • 并发编程

  • 性能调优

  • java8语法

  • lombok

  • 日志

  • 工具类

  • spring

    • Spring源码整体脉络介绍及源码编译
    • Ioc容器加载过程-Bean的生命周期源码深度剖析
    • 内置后置PostProcess处理器深度讲解
    • Untitled
    • IOC-Endgame
    • Spring-AOP源码探究
      • 动态代理
      • aop
        • Advice
        • Advisor
        • Adviced
      • spring动态增强插件
        • 背景与需求
    • 基于注解构建项目
    • springmvc原理深度解析
    • SpringMVC常见面试题总结
    • Spring常见面试题总结
    • spring5基础
    • spring注解驱动开发笔记
    • webflux基础
    • spring工厂
    • Spring AOP 编程
    • Spring 持久层
    • Spring 注解编程
    • spring整合springmvc
  • mybatis

  • springboot

  • redis

  • zookeeper

  • springcloud

  • dubbo

  • netty

  • springsecurity

  • mq消息中间件

  • shiro

  • beetle

  • 模板引擎

  • jpa

  • 数据结构与算法

  • 数据库知识与设计

  • gradle

  • maven

  • bus

  • 定时任务

  • docker

  • centos

  • 加解密

  • biz业务

  • pigx项目

  • 开源项目

  • 品达通用权限项目-黑马

  • 货币交易项目coin-尚学堂

  • php

  • backend
  • spring
shollin
2021-08-25
目录

Spring-AOP源码探究

  • 动态代理
  • aop
    • Advice
    • Advisor
    • Adviced
  • spring动态增强插件
    • 背景与需求

# 动态代理

java自带的动态代理

public class JavaProxyTest {

    public static void main(String[] args) {

        Cat cat = new Cat();
        Animal animal = (Animal) Proxy.newProxyInstance(JavaProxyTest.class.getClassLoader(), new Class[]{Animal.class}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("前置");
                try {
                    Object o = method.invoke(cat,args);
                    System.out.println("后置");
                    return o;
                }catch (Exception e){
                    System.out.println("异常通知");
                }
                return null;
            }
        });
        System.out.println(animal.getName());
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

源码主要在 Proxy类的静态类 ProxyClassFactory的apply方法

image-20211002112359600

image-20211002112449629

# aop

image-20211003104953988

public class AopTest {


    @Test
    public void createProxyByAdvice(){
        Cat cat = new Cat();
        ProxyFactory proxyFactory = new ProxyFactory(cat);
        CountingBeforeAdvice beforeAdvice = new CountingBeforeAdvice();
        // advice: 拦截通知行为,所有的方法
        proxyFactory.addAdvice(beforeAdvice);

        Animal proxy = (Animal) proxyFactory.getProxy();
        proxy.getName();
    }

    @Test
    public void createProxyByAdvisor(){
        Cat cat = new Cat();
        ProxyFactory proxyFactory = new ProxyFactory(cat);

        // advice: 拦截通知行为
        CountingBeforeAdvice beforeAdvice = new CountingBeforeAdvice();
        // advisor: 连接点和通知的适配器
        Advisor advisor = new DefaultPointcutAdvisor(beforeAdvice);

        proxyFactory.addAdvisor(advisor);
        Animal proxy = (Animal) proxyFactory.getProxy();
        proxy.getName();
        assertThat(AopUtils.isJdkDynamicProxy(proxy)).as("Proxy is a JDK proxy").isTrue();
        System.out.println(proxy);
    }

    @Test
    public void createProxyByAspectJ(){
        Cat cat = new Cat();
        ProxyFactory proxyFactory = new ProxyFactory(cat);

        // advice: 拦截通知行为
        CountingBeforeAdvice beforeAdvice = new CountingBeforeAdvice();
        // advisor: 连接点和通知的适配器
        AspectJExpressionPointcutAdvisor advisor = new AspectJExpressionPointcutAdvisor();
        advisor.setAdvice(beforeAdvice);
        advisor.setExpression("execution(* *.getName(..))");

        // advice: 切面
        proxyFactory.addAdvisor(advisor);

        Animal proxy = (Animal) proxyFactory.getProxy();
        assertThat(AopUtils.isJdkDynamicProxy(proxy)).as("Proxy is a JDK proxy").isTrue();
        proxy.getName();
    }
}
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

# Advice

拦截所有的通知方法

# Advisor

连接点和通知的适配器

# Adviced

包括advice和advisor

@Test
public void createProxyByAdvised(){
    Cat cat = new Cat();
    ProxyFactory proxyFactory = new ProxyFactory(cat);

    // advice: 拦截通知行为
    CountingBeforeAdvice beforeAdvice = new CountingBeforeAdvice();
    // advisor: 连接点和通知的适配器
    AspectJExpressionPointcutAdvisor advisor = new AspectJExpressionPointcutAdvisor();
    advisor.setAdvice(beforeAdvice);
    advisor.setExpression("execution(* *.getName(..))");

    Animal proxy = (Animal) proxyFactory.getProxy();
    Advised advised = (Advised) proxy;

    advised.addAdvisor(advisor);
    assertThat(AopUtils.isJdkDynamicProxy(proxy)).as("Proxy is a JDK proxy").isTrue();
    proxy.getName();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

image-20211003105105881

image-20211003105341610

# spring动态增强插件

# 背景与需求

image-20211003095246755

IOC-Endgame
基于注解构建项目

← IOC-Endgame 基于注解构建项目→

最近更新
01
国际象棋
09-15
02
成语
09-15
03
自然拼读
09-15
更多文章>
Theme by Vdoing | Copyright © 2019-2023 zxpnet | 粤ICP备14079330号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式