动态代理:
现在我们需要给一些类的前后增加一些功能,但是又不改变这个类;有两种方式:
1.静态代理
可以给每一个类都编写对应的代理类,并且实现和目标类相同的接口:代理对象=增强代码+原有对象;但这样是需要我们每有一个目标类就要实现一个代理类
2.动态代理
创建动态代理类会使用到java.lang.reflect.Proxy
类和java.lang.reflect.InvocationHandler
接口。
proxy类的官方翻译是这样的:
Proxy提供用于创建动态代理类和实例的静态方法,它也是由这些方法创建的所有动态代理类的超类。
其中有getProxyClass
这样的静态方法:传入一个类加载和一组接口,返回代理class对象
public static Class<?> getProxyClass(ClassLoader loader, Class<?>... interfaces) throws IllegalArgumentException{
final Class<?>[] intfs = interfaces.clone();//将类结构信息克隆一份
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {checkProxyAccess(Reflection.getCallerClass(), loader, intfs);
}
return getProxyClass0(loader, intfs);
}
简单的为某个接口创建代理:
InvocationHandler handler = new MyInvocationHandler(...);
Class<?> proxyClass = Proxy.getProxyClass(Foo.class.getClassLoader(), Foo.class);
Foo f = (Foo) proxyClass.getConstructor(InvocationHandler.class).newInstance(handler);
Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),new Class<?>[] { Foo.class },handler);
创建了代理对象,java.lang.reflect.InvocationHandler
接口可以调用Proxy
类生成的代理类方法,
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable;
因为我们在创建代理对象时,默认传入了一个InvocationHandler类,并且代理对象的每个内部方法都可以调用InvocationHandler.Invoke()类去访问目标类
示例
目标类:
package com.example;
import java.io.Serializable;
public interface Plus extends Serializable {public void add(int a, int b);}
代理类:
package com.example;
public class PlusImpl implements Plus{
@Override
public void add(int a, int b){System.out.println(a+b);}
}
增强代码:
package com.example;
import java.lang.reflect.Proxy;
import java.lang.reflect.*;
public class ProxyTest {
public static void main(String[] args) throws Exception {
/**
* @param loader 定义动态代理类的类加载器
* @param interface 定动态代理类的类需要实现的接口数组
**/
Class PlusProxyTest = Proxy.getProxyClass(PlusImpl.class.getClassLoader(), Plus.class);
//通过反射Plus构造器来创建动态代理类实例
Constructor constructor = PlusProxyTest.getConstructor(InvocationHandler.class);
Plus plusproxyImpl = (Plus) constructor.newInstance(new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//new新对象
PlusImpl plusproxyImpl1 = new PlusImpl();
//反射调用执行方法,并返回结果
return method.invoke(plusproxyImpl1,args);
}
});
System.out.println("执行方法开始");
plusproxyImpl.add(1,2);
System.out.println("执行方法结束 ");
}
}
Output:
执行方法开始
3
执行方法结束
这里看到invoke方法里没有传进去目标参数,所以需要我们手动创建一个新的对象,如果代理一个新对象还要重写invoke方法所以我们把目标对象当作参数传进来:
package com.example;
import java.lang.reflect.Proxy;
import java.lang.reflect.*;
public class ProxyTest {
public static void main(String[] args) throws Throwable {
PlusImpl target = new PlusImpl();
Plus plusproxyImpl1 = (Plus)getProxy(target);
plusproxyImpl1.add(1, 2);
}
private static Object getProxy(final Object target )throws Exception {
Class PlusProxyTest = Proxy.getProxyClass(target.getClass().getClassLoader(), target.getClass().getInterfaces());
Constructor constructor = PlusProxyTest.getConstructor(InvocationHandler.class);
Object plusproxyImpl = constructor.newInstance(new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result = method.invoke(target,args);
System.out.println(method.getName() + "方法开始执行...");
System.out.println(method.getName() + "方法执行结束...");
return result;
}
});
return plusproxyImpl;
}
}
Proxy.newProxyInstance()可以返回代理实例
package com.example;
import java.lang.reflect.Proxy;
import java.lang.reflect.*;
public class ProxyTest {
public static void main(String[] args) throws Throwable {
PlusImpl target = new PlusImpl();
Plus plusproxyImpl1 = (Plus) getProxy(target);
plusproxyImpl1.add(1, 2);
}
private static Object getProxy(final Object target) throws Exception {
Object PlusProxyTest = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(),
new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result = method.invoke(target, args);
System.out.println(method.getName() + "方法开始执行...");
System.out.println(method.getName() + "方法执行结束...");
return result;
}
});
return PlusProxyTest;
}
}
Java动态代理是为了让现有的类在不改变自己代码的情况下,对这个类进行功能增强;这好比于python的装饰器,装饰器也是为其他函数增加功能的,但少了代理对象这个中间类;
需求:在不修改源代码的情况下为其添加统计运行时间的功能
def index(x, y):
print("index %s,%s" % (x, y))
index(111, 222)
import time
def index(x,y,z):
time.sleep(3)
print("index %s,%s,%s" % (x,y,z))
def outter(func):
def timee(*args,**kwargs):
start = time.time()
func (*args,**kwargs)
stop = time.time()
print(stop - start)
return timee
index= outter(index)
index(1,2,3)
Output:
index 1,2,3
3.001108407974243
参考链接
Java 动态代理:https://www.zhihu.com/question/20794107/answer/658139129
python装饰器:https://blog.youkuaiyun.com/weixin_45644323/article/details/108392267?spm=1001.2014.3001.5501