我是靠谱客的博主 悦耳枫叶,最近开发中收集的这篇文章主要介绍18-深入理解代理模式,jdk代理模式,cglib代理模式1. 目的2. 类图3. 应用场景4. 优缺点5.静态代理6. JDK动态代理7. cglib代理模式8. 参考资料,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
文章目录
- 1. 目的
- 2. 类图
- 3. 应用场景
- 4. 优缺点
- 5.静态代理
- 6. JDK动态代理
- 6.1 Mybatis中的应用
- 7. cglib代理模式
- 8. 参考资料
1. 目的
代理模式是一种结构型设计模式, 让你能够提供对象的替代品或其占位符。 代理控制着对于原对象的访问, 并允许在将请求提交给对象前后进行一些处理。
2. 类图
3. 应用场景
- 延迟初始化
- 访问控制
- 记录日志
- 事务处理
- spring的aop
4. 优缺点
5.静态代理
/**
* Bird类实现接口Flyable,鸟能飞 。
* <p>问题1:我想统计一下fly()运行多久,简单的方法是修改源码</p>
* <p>问题2:如果不能修改源码呢,又怎么办。办法是继承。</p>
* <p>问题3:如果我再想打印日志,怎么办,再来一个类. 虽然能解决问题,但是如果我想做权限拦截,又要再生成一个类,如果
* 我想既实现日志打印,又想统计时间,又要再生成一个类,这样类越来越多,维护和扩展及其糟糕</p>
* <p>问题3的解决办法是代理,直接持有Bird实例,这个虽然能解决类爆炸问题,但是没有功能组合问题,解决方法是用接口替换</p>
* <p>问题4:如果有很多方法需要打印方法前后日志怎么办,办法:动态代理</p>
*
* @author tobebetter9527
* @create 2021/07/31 19:47
*/
public class Main {
public static void main(String[] args) {
Bird bird = new Bird();
BirdTime birdTime = new BirdTime(bird);
BirdLog birdLog = new BirdLog(birdTime);
birdLog.fly();
System.out.println("--------------------");
BirdLog birdLog1 = new BirdLog(bird);
BirdTime birdTime1 = new BirdTime(birdLog1);
birdTime1.fly();
}
}
class BirdLog implements Flyable {
private Flyable bird;
public BirdLog(Flyable bird) {
this.bird = bird;
}
@Override
public void fly() {
System.out.println("开始打印日志");
bird.fly();
System.out.println("结束打印日志");
}
}
class BirdTime implements Flyable {
private Flyable bird;
public BirdTime(Flyable bird) {
this.bird = bird;
}
@Override
public void fly() {
long start = System.currentTimeMillis();
bird.fly();
long end = System.currentTimeMillis();
System.out.println("Run time is " + (end - start));
}
}
class Bird implements Flyable {
@Override
public void fly() {
System.out.println("I can fly...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
interface Flyable {
void fly();
}
6. JDK动态代理
动态代理利用了JDK API,动态地在内存中构建代理对象,从而实现对目标对象的代理功能。
静态代理与动态代理的区别主要在:
- 静态代理在编译时就已经实现,编译完成后代理类是一个实际的class文件。
- 动态代理是在运行时动态生成的,即编译完成后没有实际的class文件,而是在运行时动态生成类字节码,并加载到JVM中。
/**
* Bird类实现接口Flyable,鸟能飞 。
* <p>问题1:我想统计一下fly()运行多久,简单的方法是修改源码</p>
* <p>问题2:如果不能修改源码呢,又怎么办。办法是继承。</p>
* <p>问题3:如果我再想打印日志,怎么办,再来一个类. 虽然能解决问题,但是如果我想做权限拦截,又要再生成一个类,如果
* 我想既实现日志打印,又想统计时间,又要再生成一个类,这样类越来越多,维护和扩展及其糟糕</p>
* <p>问题3的解决办法是代理,直接持有Bird实例,这个虽然能解决类爆炸问题,但是没有功能组合问题,解决方法是用接口替换</p>
* <p>问题4:如果有很多方法需要打印方法前后日志怎么办,办法:动态代理</p>
* <p>AOP</p>
*
* @author tobebetter9527
* @create 2021/07/31 19:47
*/
public class Main {
public static void main(String[] args) {
Bird bird = new Bird();
// jdk11,自动生成代理类源码
System.getProperties().put("jdk.proxy.ProxyGenerator.saveGeneratedFiles", "true");
Flyable instance = (Flyable) Proxy
.newProxyInstance(Bird.class.getClassLoader(), new Class[]{Flyable.class}, new BirdLog(bird));
instance.fly();
}
}
class BirdLog implements InvocationHandler {
private Flyable bird;
public BirdLog(Flyable bird) {
this.bird = bird;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
before();
Object invoke = method.invoke(bird, args);
after();
return invoke;
}
private void before() {
System.out.println("开始打印日志");
}
private void after() {
System.out.println("结束打印日志");
}
}
class Bird implements Flyable {
@Override
public void fly() {
System.out.println("I can fly...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
interface Flyable {
void fly();
}
以上代码需要用jdk11会自动生成以下代理类,如果没有jdk11,参考以下文章:
- JDK-查看动态代理生成的源码
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;
final class $Proxy0 extends Proxy implements Flyable {
private static Method m1;
private static Method m3;
private static Method m2;
private static Method m0;
public $Proxy0(InvocationHandler var1) throws {
super(var1);
}
public final boolean equals(Object var1) throws {
try {
return (Boolean)super.h.invoke(this, m1, new Object[]{var1});
} catch (RuntimeException | Error var3) {
throw var3;
} catch (Throwable var4) {
throw new UndeclaredThrowableException(var4);
}
}
public final void fly() throws {
try {
super.h.invoke(this, m3, (Object[])null);
} catch (RuntimeException | Error var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
}
public final String toString() throws {
try {
return (String)super.h.invoke(this, m2, (Object[])null);
} catch (RuntimeException | Error var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
}
public final int hashCode() throws {
try {
return (Integer)super.h.invoke(this, m0, (Object[])null);
} catch (RuntimeException | Error var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
}
static {
try {
m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));
m3 = Class.forName("com.freedom.pattern.proxy.proxy.v9.Flyable").getMethod("fly");
m2 = Class.forName("java.lang.Object").getMethod("toString");
m0 = Class.forName("java.lang.Object").getMethod("hashCode");
} catch (NoSuchMethodException var2) {
throw new NoSuchMethodError(var2.getMessage());
} catch (ClassNotFoundException var3) {
throw new NoClassDefFoundError(var3.getMessage());
}
}
}
6.1 Mybatis中的应用
- 常见的DAO或者Repository的接口如下:
public interface UserRepository extends BaseMapper<UserDO> {
List<UserDO> listAll();
}
查看其spring bean的示例:
可以看到是一个MybatisMapperProxy类型。
这只是一个接口,mybatis是如何帮我们链接数据库,查询数据,并组装返回来的呢?
- 启动工程,在MapperFactoryBean中打断点,按F7进入
- 跟踪到SqlSessionTemplate
- 跟踪到MybatisConfiguration
- 跟踪到MybatisMapperRegistry
- 跟踪到MybatisMapperProxyFactory
- 跟踪到MybatisMapperProxy
可以看到MybatisMapperProxy实现了InvocationHandler接口。 同时可以看到帮我们注入了sqlSession. 节省了很多代码。
代码地址:https://github.com/tobebetter9527/freedom-framework
7. cglib代理模式
cglib特点
- JDK的动态代理有一个限制,就是使用动态代理的对象必须实现一个或多个接口。
- 如果想代理没有实现接口的类,就可以使用CGLIB实现。
- CGLIB是一个强大的高性能的代码生成包,它可以在运行期扩展Java类与实现Java接口。
它广泛的被许多AOP的框架使用,例如Spring AOP和dynaop,为他们提供方法的interception(拦截)。 - CGLIB包的底层是通过使用一个小而快的字节码处理框架ASM,来转换字节码并生成新的类。
不鼓励直接使用ASM,因为它需要你对JVM内部结构包括class文件的格式和指令集都很熟悉。
cglib与动态代理最大的区别就是:
- 使用动态代理的对象必须实现一个或多个接口
- 使用cglib代理的对象则无需实现接口,达到代理类无侵入。
在maven的pom.xml文件中加入
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.3.0</version>
</dependency>
import java.lang.reflect.Method;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
/**
* cglib无需接口就可以生成代理类
*
* @author tobebetter9527
* @create 2021/08/01 9:40
*/
public class Main {
public static void main(String[] args) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(Bird.class);
enhancer.setCallback(new BirdLogInterceptor());
Bird bird = (Bird) enhancer.create();
bird.fly();
}
}
class BirdLogInterceptor implements MethodInterceptor {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
System.out.println(obj.getClass().getSuperclass().getName());
System.out.println("开始打印日志");
Object o = proxy.invokeSuper(obj, args);
System.out.println("结束打印日志");
return o;
}
}
class Bird {
public void fly() {
System.out.println("I can fly ...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
生成源码参考:
- spring查看生成的cglib代理类源码详解
另外查看代理类源码方法:
public class TestAop {
public static void main(String[] args) throws Exception {
saveGeneratedCGlibProxyFiles(System.getProperty("user.dir") + "/proxy");
ApplicationContext ac = new ClassPathXmlApplicationContext("META-INF/aop.xml");
MyCalculator bean = ac.getBean(MyCalculator.class);
System.out.println(bean.toString());
bean.add(1, 1);
bean.sub(1, 1);
}
public static void saveGeneratedCGlibProxyFiles(String dir) throws Exception {
Field field = System.class.getDeclaredField("props");
field.setAccessible(true);
Properties props = (Properties) field.get(null);
//dir为保存文件路径
System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, dir);
props.put("net.sf.cglib.core.DebuggingClassWriter.traceEnabled", "true");
}
}
代理类源码代码如下:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.freedom.pattern.proxy.cglib;
import java.lang.reflect.Method;
import net.sf.cglib.core.ReflectUtils;
import net.sf.cglib.core.Signature;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.Factory;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
public class Bird$$EnhancerByCGLIB$$3ca31ce7 extends Bird implements Factory {
private boolean CGLIB$BOUND;
public static Object CGLIB$FACTORY_DATA;
private static final ThreadLocal CGLIB$THREAD_CALLBACKS;
private static final Callback[] CGLIB$STATIC_CALLBACKS;
private MethodInterceptor CGLIB$CALLBACK_0;
private static Object CGLIB$CALLBACK_FILTER;
private static final Method CGLIB$fly$0$Method;
private static final MethodProxy CGLIB$fly$0$Proxy;
private static final Object[] CGLIB$emptyArgs;
private static final Method CGLIB$equals$1$Method;
private static final MethodProxy CGLIB$equals$1$Proxy;
private static final Method CGLIB$toString$2$Method;
private static final MethodProxy CGLIB$toString$2$Proxy;
private static final Method CGLIB$hashCode$3$Method;
private static final MethodProxy CGLIB$hashCode$3$Proxy;
private static final Method CGLIB$clone$4$Method;
private static final MethodProxy CGLIB$clone$4$Proxy;
public Bird$$EnhancerByCGLIB$$3ca31ce7() {
CGLIB$BIND_CALLBACKS(this);
}
static {
CGLIB$STATICHOOK1();
}
public final boolean equals(Object var1) {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (var10000 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
if (var10000 != null) {
Object var2 = var10000.intercept(this, CGLIB$equals$1$Method, new Object[]{var1}, CGLIB$equals$1$Proxy);
return var2 == null ? false : (Boolean)var2;
} else {
return super.equals(var1);
}
}
public final String toString() {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (var10000 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
return var10000 != null ? (String)var10000.intercept(this, CGLIB$toString$2$Method, CGLIB$emptyArgs, CGLIB$toString$2$Proxy) : super.toString();
}
public final int hashCode() {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (var10000 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
if (var10000 != null) {
Object var1 = var10000.intercept(this, CGLIB$hashCode$3$Method, CGLIB$emptyArgs, CGLIB$hashCode$3$Proxy);
return var1 == null ? 0 : ((Number)var1).intValue();
} else {
return super.hashCode();
}
}
protected final Object clone() throws CloneNotSupportedException {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (var10000 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
return var10000 != null ? var10000.intercept(this, CGLIB$clone$4$Method, CGLIB$emptyArgs, CGLIB$clone$4$Proxy) : super.clone();
}
public Object newInstance(Callback var1) {
CGLIB$SET_THREAD_CALLBACKS(new Callback[]{var1});
Bird$$EnhancerByCGLIB$$3ca31ce7 var10000 = new Bird$$EnhancerByCGLIB$$3ca31ce7();
CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
return var10000;
}
public Object newInstance(Class[] var1, Object[] var2, Callback[] var3) {
CGLIB$SET_THREAD_CALLBACKS(var3);
Bird$$EnhancerByCGLIB$$3ca31ce7 var10000 = new Bird$$EnhancerByCGLIB$$3ca31ce7;
switch(var1.length) {
case 0:
var10000.<init>();
CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
return var10000;
default:
throw new IllegalArgumentException("Constructor not found");
}
}
public Object newInstance(Callback[] var1) {
CGLIB$SET_THREAD_CALLBACKS(var1);
Bird$$EnhancerByCGLIB$$3ca31ce7 var10000 = new Bird$$EnhancerByCGLIB$$3ca31ce7();
CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
return var10000;
}
public void setCallbacks(Callback[] var1) {
this.CGLIB$CALLBACK_0 = (MethodInterceptor)var1[0];
}
public void setCallback(int var1, Callback var2) {
switch(var1) {
case 0:
this.CGLIB$CALLBACK_0 = (MethodInterceptor)var2;
default:
}
}
public final void fly() {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (var10000 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
if (var10000 != null) {
var10000.intercept(this, CGLIB$fly$0$Method, CGLIB$emptyArgs, CGLIB$fly$0$Proxy);
} else {
super.fly();
}
}
public Callback getCallback(int var1) {
CGLIB$BIND_CALLBACKS(this);
MethodInterceptor var10000;
switch(var1) {
case 0:
var10000 = this.CGLIB$CALLBACK_0;
break;
default:
var10000 = null;
}
return var10000;
}
public Callback[] getCallbacks() {
CGLIB$BIND_CALLBACKS(this);
return new Callback[]{this.CGLIB$CALLBACK_0};
}
public static void CGLIB$SET_STATIC_CALLBACKS(Callback[] var0) {
CGLIB$STATIC_CALLBACKS = var0;
}
public static void CGLIB$SET_THREAD_CALLBACKS(Callback[] var0) {
CGLIB$THREAD_CALLBACKS.set(var0);
}
static void CGLIB$STATICHOOK1() {
CGLIB$THREAD_CALLBACKS = new ThreadLocal();
CGLIB$emptyArgs = new Object[0];
Class var0 = Class.forName("com.freedom.pattern.proxy.cglib.Bird$$EnhancerByCGLIB$$3ca31ce7");
Class var1;
Method[] var10000 = ReflectUtils.findMethods(new String[]{"equals", "(Ljava/lang/Object;)Z", "toString", "()Ljava/lang/String;", "hashCode", "()I", "clone", "()Ljava/lang/Object;"}, (var1 = Class.forName("java.lang.Object")).getDeclaredMethods());
CGLIB$equals$1$Method = var10000[0];
CGLIB$equals$1$Proxy = MethodProxy.create(var1, var0, "(Ljava/lang/Object;)Z", "equals", "CGLIB$equals$1");
CGLIB$toString$2$Method = var10000[1];
CGLIB$toString$2$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/String;", "toString", "CGLIB$toString$2");
CGLIB$hashCode$3$Method = var10000[2];
CGLIB$hashCode$3$Proxy = MethodProxy.create(var1, var0, "()I", "hashCode", "CGLIB$hashCode$3");
CGLIB$clone$4$Method = var10000[3];
CGLIB$clone$4$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/Object;", "clone", "CGLIB$clone$4");
CGLIB$fly$0$Method = ReflectUtils.findMethods(new String[]{"fly", "()V"}, (var1 = Class.forName("com.freedom.pattern.proxy.cglib.Bird")).getDeclaredMethods())[0];
CGLIB$fly$0$Proxy = MethodProxy.create(var1, var0, "()V", "fly", "CGLIB$fly$0");
}
private static final void CGLIB$BIND_CALLBACKS(Object var0) {
Bird$$EnhancerByCGLIB$$3ca31ce7 var1 = (Bird$$EnhancerByCGLIB$$3ca31ce7)var0;
if (!var1.CGLIB$BOUND) {
var1.CGLIB$BOUND = true;
Object var10000 = CGLIB$THREAD_CALLBACKS.get();
if (var10000 == null) {
var10000 = CGLIB$STATIC_CALLBACKS;
if (var10000 == null) {
return;
}
}
var1.CGLIB$CALLBACK_0 = (MethodInterceptor)((Callback[])var10000)[0];
}
}
final boolean CGLIB$equals$1(Object var1) {
return super.equals(var1);
}
final int CGLIB$hashCode$3() {
return super.hashCode();
}
final Object CGLIB$clone$4() throws CloneNotSupportedException {
return super.clone();
}
final String CGLIB$toString$2() {
return super.toString();
}
final void CGLIB$fly$0() {
super.fly();
}
public static MethodProxy CGLIB$findMethodProxy(Signature var0) {
String var10000 = var0.toString();
switch(var10000.hashCode()) {
case -1271409118:
if (var10000.equals("fly()V")) {
return CGLIB$fly$0$Proxy;
}
break;
case -508378822:
if (var10000.equals("clone()Ljava/lang/Object;")) {
return CGLIB$clone$4$Proxy;
}
break;
case 1826985398:
if (var10000.equals("equals(Ljava/lang/Object;)Z")) {
return CGLIB$equals$1$Proxy;
}
break;
case 1913648695:
if (var10000.equals("toString()Ljava/lang/String;")) {
return CGLIB$toString$2$Proxy;
}
break;
case 1984935277:
if (var10000.equals("hashCode()I")) {
return CGLIB$hashCode$3$Proxy;
}
}
return null;
}
}
8. 参考资料
- https://refactoringguru.cn/design-patterns/proxy
- https://segmentfault.com/a/1190000011291179
最后
以上就是悦耳枫叶为你收集整理的18-深入理解代理模式,jdk代理模式,cglib代理模式1. 目的2. 类图3. 应用场景4. 优缺点5.静态代理6. JDK动态代理7. cglib代理模式8. 参考资料的全部内容,希望文章能够帮你解决18-深入理解代理模式,jdk代理模式,cglib代理模式1. 目的2. 类图3. 应用场景4. 优缺点5.静态代理6. JDK动态代理7. cglib代理模式8. 参考资料所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复