首页 技术 正文
技术 2022年11月15日
0 收藏 464 点赞 4,451 浏览 1982 个字

先举个静态代理的例子,可能多少有些不恰当,不过本次学习记录,重点不在于通信协议。 比如你在一个机房里,你不能联网,只能连通过一台能连公网的代理机器上网。你发送了一个http请求,将由代理帮你上网。

首先有一个HttpMessage接口,InternetProxy(公网代理)和LANMessage(局域网发消息) 都实现HttpMessage接口。公网代理将代理LANMessage上网。实际上代理模式,增强了原来对象的方法,并且无侵入性,不需要修改原有代码。

 public interface HttpMessage {
void sendMsg(String msg);
}
 public class LANMessage implements  HttpMessage {
@Override
public void sendMsg(String msg) {
System.out.println("lan send msg");
}
}
 public class InternetProxy implements HttpMessage {     //被代理的对象
LANMessage message; @Override
public void sendMsg(String msg) {
before();
message.sendMsg(msg);
after();
} void before() {
System.out.println("prepare internet msg");
} void after() {
System.out.println("after internet msg");
}
}

上面只是一个静态代理增强对象的示例。当代码中有大量的 需要增强,甚至动态增强的代码时,这种代理类,会遍布项目中到处都是,并且当接口改变,代理类和实现类都要一并修改。所以有了动态代理。

jdk动态代理:

无需创建代理类,直接使用,会生成class文件,class文件也将被加载到内存中的Class对象,有了class对象,就有了类的所有信息,在调用方法的时候,动态代理相当于拦截了我们所有的调用,你可以在invoke中对目标对象和其方法进行增强。也应该不难想到,new instance 基本上就是通过拿到的class对象,来拿到其Contrustor 来构造对象。如果反编译生成的class文件,也可以看到在调用方法的时候,刚实现的invoke方法会被调用。 Class对象在手,天下我有~

 public class TestProxy {     LANMessage msg=new LANMessage();
@Test
public void fun(){
HttpMessage message=(HttpMessage) Proxy.newProxyInstance(LANMessage.class.getClassLoader(), LANMessage.class.getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("before");
Object res= method.invoke(msg,args);
System.out.println("after");
return res;
}
});
message.sendMsg("hello");
}
}

CGLib方式:

该方式可代理任何类 不需要提供接口。其生成的class字节码文件,反编译后可以看到 它生成的是委托类的子类。

 public class TestProxy {     LANMessage msg = new LANMessage();     @Test
public void fun() { CGLibProxy proxy=new CGLibProxy();
HttpMessage msg=(HttpMessage) proxy.getProxy(LANMessage.class);
msg.sendMsg("hi");
}
} class CGLibProxy implements MethodInterceptor { public Object getProxy(Class<?> clazz) {
return Enhancer.create(clazz, this);
} @Override
public Object intercept(Object o, Method method, Object[] args, MethodProxy proxy) throws Throwable {
System.out.println("before");
Object res = proxy.invokeSuper(o, args);
System.out.println("afert");
return res;
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,486
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,126
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,287