笔记

1. 什么是AOP?

Aspect-Oriented Programming 面向切面编程,关注一个统一的切面,相对于OOP(面向对象编程)。

适合的场景

  • 日志
  • 缓存
  • 鉴权

如果用OOP来做怎么办?

  • 装饰器模式

2. 装饰器模式 (OOP, 静态代理)

Decorator pattern:

动态地为一个对象增加功能,但是不改其结构。

public interface DataService {
String a(int i); String b(int i);
} public class DataServiceImpl implements DataService {
@Override
public String a(int i) {
return UUID.randomUUID().toString();
} @Override
public String b(int i) {
return UUID.randomUUID().toString();
}
} public class Main {
static DataService service = new CacheDecorator(new LogDecorator(new DataServiceImpl())); public static void main(String[] args) {
System.out.println(service.a(1));
System.out.println(service.b(1));
}
}

假如是class,就会有浪费。

3. AOP动态代理

public class LogProxy implements InvocationHandler {
private DataService delegate; public LogProxy(DataService delegate) {
this.delegate = delegate;
} @Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println(method.getName() + " is invoker " + Arrays.toString(args));
Object result = method.invoke(delegate, args);
System.out.println(method.getName() + " is finished: " + result);
return result;
}
} public class Main {
static DataService service = new DataServiceImpl(); public static void main(String[] args) {
DataService dataService = (DataService) Proxy.newProxyInstance(
service.getClass().getClassLoader(),
new Class[]{DataService.class},
new LogProxy(service));
dataService.a(1);
dataService.b(3);
}
}

优点:方便,不需要依赖任何第三方库

缺点:功能受限,只能用于接口 (源代码中接收参数为interface,如果是class会报异常)

4. 如果是class怎么办?

CGLIB/ByteBuddy字节码生成

public class Main {
static DataServiceImpl service = new DataServiceImpl(); public static class LogInterceptor implements MethodInterceptor {
private DataServiceImpl delegate; public LogInterceptor(DataServiceImpl delegate) {
this.delegate = delegate;
} @Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println(method.getName() + " is invoker " + Arrays.toString(objects));
Object result = method.invoke(delegate, objects);
System.out.println(method.getName() + " is finished: " + result);
return result;
}
} public static void main(String[] args) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(DataServiceImpl.class);
enhancer.setCallback(new LogInterceptor(service));
DataServiceImpl enhancedService = (DataServiceImpl) enhancer.create();
enhancedService.a(1);
enhancedService.b(2);
} // 这里会动态生成一个叫做DataServiceImpl$$EnhancerByCGLIB$$9bce1175@651的类,它是动态生成出来,并且是继承我们的DataServiceImpl的。

优点:强大,不受接口的限制

缺点:需要额外引用第三方库,不能增强final类/final/private方法(因为原理是继承)。

动态代理,AOP和Spring的更多相关文章

  1. CgLib动态代理学习【Spring AOP基础之一】

    如果不了解JDK中proxy动态代理机制的可以先查看上篇文章的内容:Java动态代理学习[Spring AOP基础之一] 由于Java动态代理Proxy.newProxyInstance()的时候会发 ...

  2. 基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务(@Trasactional)到底有什么区别。

    基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务(@Trasactional)到底有什么区别. 我还是喜欢基于Schema风格的Spring事务管理,但也有很多人在用基于@Tras ...

  3. Spring -- <tx:annotation-driven>注解基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务(@Trasactional)的区别。

    借鉴:http://jinnianshilongnian.iteye.com/blog/1508018 基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务(@Trasactional ...

  4. Spring-Boot的动态代理AOP原理

    前言 Spring AOP使用了动态代理技术,动态代理在业界比较流行的实现方式有,CGLIB,Javassist,ASM等等. Spring动态代理实现方式 Spring采用了JDK和CGLIB两种方 ...

  5. 浅析DispatchProxy动态代理AOP

    浅析DispatchProxy动态代理AOP(代码源码) 最近学习了一段时间Java,了解到Java实现动态代理AOP主要分为两种方式JDK.CGLIB,我之前使用NET实现AOP切面编程,会用Fil ...

  6. Java动态代理学习【Spring AOP基础之一】

    Spring AOP使用的其中一个底层技术就是Java的动态代理技术.Java的动态代理技术主要围绕两个类进行的 java.lang.reflect.InvocationHandler java.la ...

  7. 动态代理以及对应Spring中AOP源码分析

    AOP(面向切面编程)在Spring中是被广泛应用的(例如日志,事务,权限等),而它的基本原理便是动态代理. 我们知道动态代理有两种:基于JDK的动态代理以及基于CGlib动态代理.以下是两种动态代理 ...

  8. spring中使用动态代理(AOP)

    spring是整合了BGLIB和JDK两种动态代理 示例:使用CGLIB代理 public class MyCar { private String color = "blue"; ...

  9. [转]JAVA的动态代理机制及Spring的实现方式

    JAVA 代理实现 代理的实现分动态代理和静态代理,静态代理的实现是对已经生成了的JAVA类进行封装. 动态代理则是在运行时生成了相关代理累,在JAVA中生成动态代理一般有两种方式. JDK自带实现方 ...

  10. 动态代理 aop切面实现事务管理

    1.定义接口和实现 public interface UserService { public String getName(int id); public Integer getAge(int id ...

随机推荐

  1. LPWAN

    典型LPWA技术: 1 Sigfox技术由同名的法国Sigfox公司设计研发,成立于2010年,因为Sigfox网络由Sigfox公司为主导进行全球部署,这样能最大程度保证网络服务质量的统一性和稳定性 ...

  2. 21. Regular Expressions--from Apache

    转自: http://jmeter.apache.org/usermanual/regular_expressions.html 21.1 Overview¶ JMeter includes the ...

  3. MySQL 远程连接问题 (Linux Server)

    Mysql Workbench 连接Ubuntu上的Mysql时报如下错误: 原因:查看  /etc/mysql/mysql.conf.d/mysqld.cnf # # Instead of skip ...

  4. Flask - 运行APP

    from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return 'Hello, ...

  5. BigOps自动化运维安装以及所遇故障处理

    本文参考官方文档进行安装,以及在安装中所遇到的问题呈现给大家.废话就不说了,开始安装.一.准备工作:本机系统环境是CentOS 7 x86 64位硬件配置建议物理内存8G+.CPU 4 cores+. ...

  6. Write-Up-wakanda-1

    关于 下载地址:点我 哔哩哔哩:哔哩哔哩 祖传开头 信息收集 这里用vm虚拟机可能有一点问题,因为官方的是用vbox虚拟机导出的镜像文件.所以这次使用vbox虚拟机. ➜ ~ ip a show de ...

  7. 【转】CGI 和 FastCGI 协议的运行原理

    介绍 深入CGI协议 CGI的运行原理 CGI协议的缺陷 深入FastCGI协议 FastCGI协议运行原理 为什么是 FastCGI 而非 CGI 协议 CGI 与 FastCGI 架构 再看 Fa ...

  8. Windows 10 20H1版名称被定为Windows 10 Version 2004版以示区分

    导读 我们知道Windows 10 20H1 版目前的开发工作已经接近完成,当前微软主要通过新版本来修复部分已知的问题. 而名称上面按照以往规律推算应该是 Windows 10 Version 200 ...

  9. One-Hot Encoding(独热编码)

    前几天查了一些与独热编码相关的资料后,发现看不进去...看不太懂,今天又查了一下,然后写了写代码,通过自己写例子加上别人的解释后,从结果上观察,明白了sklearn中独热编码做了什么事. 下面举个例子 ...

  10. CSP-J2019 加工零件

    Background: 之前 $noip $死了,泥萌都说 \(noip SPFA\) 了,现在 \(noip\) 复活了,所以 \(SPFA\) 也复活了. (注:这里的 \(noip\) 跟 \( ...