代理模式
解说:给某一个对象提供一个代理,并由代理对象控制对原对象的引用;
代理模式需要以下几个角色:
1 主题:规定代理类和真实对象共同对外暴露的接口;
2 代理类:专门代理真实对象的类;
3 真实对象:需要被代理的对象;
代理解决的主要的业务就是需要在 真实对象的某个接口 前后处理一些事情,框架中多会用到这种功能,比如 打日志、记录时间等
静态代理
静态代理是指自己动手编写代码实现代理类;
优点:业务类只需要关注业务逻辑本身,保证了业务类的重用性。这是代理的共有优点。
缺点:每一个真实对象都需要一个具体的代理类,不能做到可重用;
静态代理比较简单,下边用代码来具体说明;
主题接口:IAnimal
public interface IAnimal {
/**
* 动物叫
*/
void bark();
}
真实对象:Dog
public class Dog implements IAnimal { private String name; public Dog(String name) {
this.name = name;
} @Override
public void bark() {
System.out.println(this.name + " bark:wang wang wang ... ");
}
}
代理:DogProxy
public class DogProxy implements IAnimal {
private Dog dog; public DogProxy(Dog dog) {
this.dog = dog;
} @Override
public void bark() {
long l = System.currentTimeMillis();
System.out.println("dog will bark...");
this.dog.bark();
System.out.println("dog has barked which takes " + (System.currentTimeMillis() - l) + " ms !");
}
}
静态代理使用:
public class StaticProxyTest { public static void main(String[] args) { IAnimal dog = new Dog("大黄"); IAnimal dogProxy = new DogProxy(dog); dogProxy.bark();
}
}
代理和真实对象对外暴露一致
动态代理
动态代理是指在运行时动态生成代理类;
jdk
要使用Java中原生的动态代理,需要用到以下几个类和接口
- 接口InvocationHandler
- Proxy类
我们还是用静态代理用到的代码:主题接口IAnimal和真实对象Dog不变,去掉DogProxy和StaticProxyTest,增加以下代码
DogProxyInvocationHandler
public class DogProxyInvocationHandler implements InvocationHandler {
private Object animal; public DogProxyInvocationHandler(Object animal) {
this.animal = animal;
} @Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("proxy class:" + proxy.getClass() + ",class:" + getClass() + ",method:" + method);
Object obj = method.invoke(animal, args);
System.out.println("obj:" + obj);
return obj;
}
}
DynamicProxyTest
public class DynamicProxyTest { public static void main(String[] args) { IAnimal dog = new Dog("大黄"); InvocationHandler invocationHandler = new DogProxyInvocationHandler(dog);
IAnimal animal = (IAnimal) Proxy.newProxyInstance(dog.getClass().getClassLoader(),
dog.getClass().getInterfaces(),
invocationHandler);
animal.bark();
} }
可以看出:Java动态代理 我们必须有真实对象,实现了InvocationHandler接口的自己的处理类,然后通过Proxy生成代理类
输出如下:
proxy class:class com.sun.proxy.$Proxy0,class:class com.shock.base.proxy.dynamic.DogProxyInvocationHandler,method:public abstract void com.shock.base.proxy.dynamic.IAnimal.bark()
obj:null
这里动态代理的优势相比静态代理为:即使真实对象有N个接口,我们的invocationHandler只需要一个Invoke方法即可!
这里有几个问题:
1 动态代理生成的class name为什么是 $Proxy0 ?
如图,相关变量如下:
以上便可以解决相关问题
2 动态代理生成的代理类到底是什么样子的?生成代理类的关键接口是什么?为何调用真实对象的某个接口会进入invoke方法?
通过分析源码:我们知道 Proxy.newProxyInstance → Proxy.getProxyClass0 → WeakCache.get → WeakCache.Factory.get → Proxy.ProxyClassFactory.apply → ProxyGenerator.generateProxyClass
最终生成了一个 byte[] 类型的 class类;这样byte[] 比较抽象 ,我们想看到该怎么办?可以通过下边的代码生成Proxy0
int accessFlags = Modifier.PUBLIC | Modifier.FINAL; byte[] bytes = ProxyGenerator.generateProxyClass("com.sun.proxy.$Proxy0",
new Class[]{IAnimal.class},
accessFlags);
FileOutputStream fileOutputStream = new FileOutputStream(new File(
"~/work/$Proxy0.class")); fileOutputStream.write(bytes);
fileOutputStream.flush();
fileOutputStream.close();
生成的代码如下:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
// package com.sun.proxy; import com.shock.base.proxy.dynamic.IAnimal;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException; public final class $Proxy0 extends Proxy implements IAnimal {
private static Method m1;
private static Method m2;
private static Method m3;
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})).booleanValue();
} catch (RuntimeException | Error var3) {
throw var3;
} catch (Throwable var4) {
throw new UndeclaredThrowableException(var4);
}
} 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 void bark() throws {
try {
super.h.invoke(this, m3, (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)).intValue();
} catch (RuntimeException | Error var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
} static {
try {
m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[]{Class.forName("java.lang.Object")});
m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
m3 = Class.forName("com.shock.base.proxy.dynamic.IAnimal").getMethod("bark", new Class[0]);
m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
} catch (NoSuchMethodException var2) {
throw new NoSuchMethodError(var2.getMessage());
} catch (ClassNotFoundException var3) {
throw new NoClassDefFoundError(var3.getMessage());
}
}
}
以上代码为我们解答了红色的问题。虽然代码是这样的 代理类集成了 Proxy类,但是如果想要验证 如何验证呢?
cglib
cglib是什么?CGLIB is a powerful, high performance code generation library.
特点简单说:
- CGLib (Code Generation Library) 是一个强大的,高性能,高质量的Code生成类库;
- 它可以在运行期扩展Java类与实现Java接口;
- CGLib 比 Java 的 java.lang.reflect.Proxy 类更强的在于它不仅可以接管接口类的方法,还可以接管普通类的方法。
- CGLib 的底层是Java字节码操作框架 —— ASM
引入JAR包支持,如下:
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.4</version>
</dependency>
目前最新的版本是 3.2.4,CGLib的package分布和作用如下:
- net.sf.cglib.core:底层字节码处理类,他们大部分与ASM有关系,对其进行封装,更易于使用;
- net.sf.cglib.transform:编译期或运行期类和类文件的转换;
- net.sf.cglib.proxy:实现创建代理和方法拦截器的类;
- net.sf.cglib.reflect:实现快速反射的类;
- net.sf.cglib.util:集合排序工具类;
- net.sf.cglib.beans:JavaBean相关的工具类;
我们沿用上边的例子,来做下演示:
加入你有一个类Animal,打算对里边的所有方法进行包装,由于这个类没有实现接口,所以你无法使用jdk 动态代理
public class Animal { public void bark() {
System.out.println("i am 大黄!");
} public String singSong(String name) {
return name + " is singing!";
} public String testt(String name) {
return name + " is testing!";
}
}
你现在想要在testt方法输出前后不加任何内容,但是另外两个方法输出前后要加一个字符串,效果如下:
BEFORE
i am 大黄!
AFTER
===============================
BEFORE
老刘 is singing!
AFTER
===============================
大黄 is testing!
其中=======是分隔线,上边两个方法前后都改变了,但是最后一个方法则没做任何改变还是原生的。如何做到?
首先我们要定义一个拦截器,该拦截器实现了 cgLib的MethodInterceptor,如下:
public class AnimalWrapper implements MethodInterceptor { @Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
System.out.println("BEFORE");
Object obj2 = proxy.invokeSuper(obj, args);
System.out.println("AFTER");
return obj2;
}
}
再定义一个拦截器过滤器,如下:
public class ApiFilter implements CallbackFilter { @Override
public int accept(Method method) {
String name = method.getName();
if (name.length() == 5) {
return 1;
}
return 0;
}
}
接下来看下测试类:
public class CglibTest { public static void main(String[] args) { Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(Animal.class);
enhancer.setCallbacks(new Callback[]{new AnimalWrapper(), NoOp.INSTANCE});
enhancer.setCallbackFilter(new ApiFilter());
Animal animal = (Animal) enhancer.create(); animal.bark();
System.out.println("===============================");
animal.singSong("老刘");
System.out.println("===============================");
animal.testt("大黄");
}
}
CGLib通过 Enhancer、Callback、CallbackFilter就可以实现上述功能了。
深入代码:Enhancer → KeyFactory.Generator → AbstractClassGenerator → DefaultGeneratorStrategy.generate → KeyFactory.Generator.generateClass → ClassWriter.toByteArray 生成 class bytecode .
生成的代码反编译如下:
public class Animal$$EnhancerByCGLIB$$223151cf extends com.shock.base.proxy.cglib.Animal implements net.sf.cglib.proxy.Factory {
private boolean CGLIB$BOUND;
public static java.lang.Object CGLIB$FACTORY_DATA;
private static final java.lang.ThreadLocal CGLIB$THREAD_CALLBACKS;
private static final net.sf.cglib.proxy.Callback[] CGLIB$STATIC_CALLBACKS;
private net.sf.cglib.proxy.MethodInterceptor CGLIB$CALLBACK_0;
private net.sf.cglib.proxy.NoOp CGLIB$CALLBACK_1;
private static java.lang.Object CGLIB$CALLBACK_FILTER;
private static final java.lang.reflect.Method CGLIB$bark$0$Method;
private static final net.sf.cglib.proxy.MethodProxy CGLIB$bark$0$Proxy;
private static final java.lang.Object[] CGLIB$emptyArgs;
private static final java.lang.reflect.Method CGLIB$singSong$1$Method;
private static final net.sf.cglib.proxy.MethodProxy CGLIB$singSong$1$Proxy;
private static final java.lang.reflect.Method CGLIB$equals$3$Method;
private static final net.sf.cglib.proxy.MethodProxy CGLIB$equals$3$Proxy;
private static final java.lang.reflect.Method CGLIB$toString$4$Method;
private static final net.sf.cglib.proxy.MethodProxy CGLIB$toString$4$Proxy;
private static final java.lang.reflect.Method CGLIB$hashCode$5$Method;
private static final net.sf.cglib.proxy.MethodProxy CGLIB$hashCode$5$Proxy; static void CGLIB$STATICHOOK1() { /* compiled code */ } final void CGLIB$bark$0() { /* compiled code */ } public final void bark() { /* compiled code */ } final void CGLIB$singSong$1(java.lang.String s) { /* compiled code */ } public final void singSong(java.lang.String s) { /* compiled code */ } final boolean CGLIB$equals$3(java.lang.Object o) { /* compiled code */ } public final boolean equals(java.lang.Object o) { /* compiled code */ } final java.lang.String CGLIB$toString$4() { /* compiled code */ } public final java.lang.String toString() { /* compiled code */ } final int CGLIB$hashCode$5() { /* compiled code */ } public final int hashCode() { /* compiled code */ } public static net.sf.cglib.proxy.MethodProxy CGLIB$findMethodProxy(net.sf.cglib.core.Signature signature) { /* compiled code */ } public Animal$$EnhancerByCGLIB$$223151cf() { /* compiled code */ } public static void CGLIB$SET_THREAD_CALLBACKS(net.sf.cglib.proxy.Callback[] callbacks) { /* compiled code */ } public static void CGLIB$SET_STATIC_CALLBACKS(net.sf.cglib.proxy.Callback[] callbacks) { /* compiled code */ } private static final void CGLIB$BIND_CALLBACKS(java.lang.Object o) { /* compiled code */ } public java.lang.Object newInstance(net.sf.cglib.proxy.Callback[] callbacks) { /* compiled code */ } public java.lang.Object newInstance(net.sf.cglib.proxy.Callback callback) { /* compiled code */ } public java.lang.Object newInstance(java.lang.Class[] classes, java.lang.Object[] objects, net.sf.cglib.proxy.Callback[] callbacks) { /* compiled code */ } public net.sf.cglib.proxy.Callback getCallback(int i) { /* compiled code */ } public void setCallback(int i, net.sf.cglib.proxy.Callback callback) { /* compiled code */ } public net.sf.cglib.proxy.Callback[] getCallbacks() { /* compiled code */ } public void setCallbacks(net.sf.cglib.proxy.Callback[] callbacks) { /* compiled code */ }
}
该段代码最后实在没办法生成,是将CGLib代码源码下载,然后插入片段代码生成的。