动态代理 (JDK代理)
1、动态代理用到的类和方法
动态代理涉及到的类是java.lang.reflect.Proxy,主要是用到它的newProxyInstance方法。
public class Proxy implements java.io.Serializable { /** prefix for all proxy class names */ private final static String proxyClassNamePrefix = "$Proxy"; /** * Returns an instance of a proxy class for the specified interfaces * that dispatches method invocations to the specified invocation * handler. This method is equivalent to: * <pre> * Proxy.getProxyClass(loader, interfaces). * getConstructor(new Class[] { InvocationHandler.class }). * newInstance(new Object[] { handler }); * </pre> * * <p>{@code Proxy.newProxyInstance} throws * {@code IllegalArgumentException} for the same reasons that * {@code Proxy.getProxyClass} does. * * @param loader the class loader to define the proxy class * @param interfaces the list of interfaces for the proxy class * to implement * @param h the invocation handler to dispatch method invocations to * @return a proxy instance with the specified invocation handler of a * proxy class that is defined by the specified class loader * and that implements the specified interfaces * @throws IllegalArgumentException if any of the restrictions on the * parameters that may be passed to {@code getProxyClass} * are violated * @throws NullPointerException if the {@code interfaces} array * argument or any of its elements are {@code null}, or * if the invocation handler, {@code h}, is * {@code null} */ @CallerSensitive public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) throws IllegalArgumentException { if (h == null) { throw new NullPointerException(); } final SecurityManager sm = System.getSecurityManager(); if (sm != null) { checkProxyAccess(Reflection.getCallerClass(), loader, interfaces); } /* * Look up or generate the designated proxy class. */ Class<?> cl = getProxyClass0(loader, interfaces); /* * Invoke its constructor with the designated invocation handler. */ try { final Constructor<?> cons = cl.getConstructor(constructorParams); final InvocationHandler ih = h; if (sm != null && ProxyAccessHelper.needsNewInstanceCheck(cl)) { // create proxy instance with doPrivilege as the proxy class may // implement non-public interfaces that requires a special permission return AccessController.doPrivileged(new PrivilegedAction<Object>() { public Object run() { return newInstance(cons, ih); } }); } else { return newInstance(cons, ih); } } catch (NoSuchMethodException e) { throw new InternalError(e.toString()); } } }2、动态代理涉及到几个概念
动态代理涉及到几个概念:Proxy、proxy class、proxy interface、proxy instance、InvocationHandler、target class、target instance。
示例:明星-->经纪人,经纪人是明星的代理。
注意:target class必须实现接口。
java.lang.reflect.Proxy类提供了创建proxy classes和proxy instance的方法。java.lang.reflect.Proxy类是所有proxy classes的子类。
【Proxy与proxy class和proxy instance的关系】
Proxy provides static methods for creating dynamic proxy classes and instances, and it is also the superclass of all dynamic proxy classes created by those methods.
【proxy class】
A dynamic proxy class (simply referred to as a proxy class below) is a class that implements a list of interfaces specified at runtime when the class is created, with behavior as described below.
【proxy interface和proxy class的关系】
A proxy interface is such an interface that is implemented by a proxy class.
【proxy instance和proxy class的关系】
A proxy instance is an instance of a proxy class.
【proxy instance和invocation handler的关系】
Each proxy instance has an associated invocation handler object, which implements the interface InvocationHandler.
【proxy interface是proxy class的接口,proxy instance是proxy class的实例。当调用proxy instance的某个proxy interface的method时,会执行InvocationHandler的invoke方法。】
A method invocation on a proxy instance through one of its proxy interfaces will be dispatched to the invoke method of the instance's invocation handler, passing the proxy instance, a java.lang.reflect.Method object identifying the method that was invoked, and an array of type Object containing the arguments.
3、示例代码
To create a proxy for some interface Foo:
or more simply:
Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(), new Class[] { Foo.class }, handler);下面是newProxyInstance方法的参数
public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)假设我们正在使用一个(实现了某个接口的)实例target
loader: target.getClass().getClassLoader()
interfaces: target.getClass().getInterfaces()
假设我们正在使用一个接口Foo
loader: Foo.class.getClassLoader()
interfaces: new Class[] {Foo.class}
Java动态代理的两种实现方法:JDK动态代理和CGLIB动态代理jdk静态代理和动态代理
#yyds干货盘点# 设计模式之静态代理,jdk动态代理,cglib动态代理的区别jdk静态代理和动态代理
代理模式-动态代理详解(JDK动态代理和CGLIB动态代理)jdk cglib动态代理
代理模式-动态代理(基于接口,JDK动态代理)jdk动态代理和cglib动态代理
Java动态代理之JDK动态代理和CGLib动态代理jdk动态代理
谁与争锋,JDK动态代理大战CGLib动态代理jdk动态代理和cglib动态代理
Spring入门第三讲 Spring中的代理 静态代理 动态代理(JDK代理) cglib代理(字节码增强) Spring中的AOPspring的静态代理和动态代理
代理模式之静态代理,JDK动态代理和cglib动态代理jdk cglib动态代理
静态代理,JDK动态代理,Cglib动态代理详解jdk动态代理