1、代理设计模式的原理
使用一个代理将对象包装起来,然后用该代理替代原始对象,任何对对象的调用都要通过代理,代理对象决定是否以及何时将方法调用转到原始对象上。
2、静态代理示例
/**
* @auther hu
* @create 2019/8/18
* <p>
* 静态代理示例
* 代理类和被代理类在编译期间,就确定下来了
*/
interface ClotheFactory {
void produce();
}
class NikeClothFactory implements ClotheFactory {
@Override
public void produce() {
System.out.println("生产Nike衣服");
}
}
class ProxyClotheFactory implements ClotheFactory {
private ClotheFactory clotheFactory;
public ProxyClotheFactory(ClotheFactory clotheFactory) {
this.clotheFactory = clotheFactory;
}
@Override
public void produce() {
System.out.println("生产衣服的准备工作");
clotheFactory.produce();
System.out.println("生产衣服的后续工作");
}
}
public class ProxyTest {
public static void main(String[] args) {
NikeClothFactory nikeClothFactory = new NikeClothFactory();
ProxyClotheFactory proxy = new ProxyClotheFactory(nikeClothFactory);
proxy.produce();
}
}
3、动态代理示例
3.1、JDK动态代理
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* @auther hu
* @create 2019/8/18
* <p>
* 动态代理示例
*/
interface People {
void eat(String food);
}
//被代理类
class SuperMan implements People {
@Override
public void eat(String food) {
System.out.println("超人喜欢吃" + food);
}
}
//代理类
class ProxyFactory {
//返回一个代理对象
public static Object getProxyInstance(Object obj) {
MyInvocationHandler myInvocationHandler = new MyInvocationHandler();
myInvocationHandler.bind(obj);
return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), myInvocationHandler);
}
}
class MyInvocationHandler implements InvocationHandler {
private Object obj;
//给需要代理的对象赋值
public void bind(Object obj) {
this.obj = obj;
}
//等通过代理类的对象调用方法时,会自动的调用如下的方法
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//object 被代理的对象
Object returnVal = method.invoke(obj, args);
//方法的返回值作为invoke的返回值
return returnVal;
}
}
public class ProxyTest2 {
public static void main(String[] args) {
SuperMan superMan = new SuperMan();
People proxyInstance = (People) ProxyFactory.getProxyInstance(superMan);
proxyInstance.eat("红烧肉");
}
}
3.2、CGLIB动态代理
/**
* @auther hu .
* @create 2019/8/19
*/
// 导入jar包 cglib-version.jar asm-version.jar https://mvnrepository.com/search?q=cglib
import java.lang.reflect.Method;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
interface Man {
void sayHello();
}
class Chinese implements Man {
@Override
public void sayHello() {
System.out.println("Chinese say hello.");
}
}
class ChinesePoxy implements MethodInterceptor {
@Override
public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
System.out.println("-------- start --------");
Object intercept = methodProxy.invokeSuper(object, args);
System.out.println("-------- end ---------");
return intercept;
}
}
public class ProxyTest3 {
public static void main(String[] args) {
ChinesePoxy chinesePoxy = new ChinesePoxy();
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(Chinese.class);
enhancer.setCallback(chinesePoxy);
Chinese proxy = (Chinese) enhancer.create();
proxy.sayHello();
}
}
3.2、总结一下JDK动态代理和Gglib动态代理的区别:
- JDK动态代理是实现了被代理对象的接口,Cglib是继承了被代理对象。
- JDK和Cglib都是在运行期生成字节码,JDK是直接写Class字节码,Cglib使用ASM框架写Class字节码,Cglib代理实现更复杂,生成代理类比JDK效率低。
- JDK调用代理方法,是通过反射机制调用,Cglib是通过FastClass机制直接调用方法,Cglib执行效率更高。