当前位置:网站首页>Dynamic Agent Learning
Dynamic Agent Learning
2022-08-11 07:49:00 【LiuJia111222333】
动态代理和静态代理角色一样 动态代理的代理类是动态生成的,不是我们直接写好的 动态代理分为两大类:A dynamic proxy for the base interface,Dynamic proxy for base classes
There are three ways to implement dynamic proxy:
- 基于接口---jdk动态代理
- 基于类:cglib
- java字节码实现:javassit
需要了解两个类:Proxy(代理类),InvocationHandler (调用处理程序)
InvocationHandler
InvocationHandler 是代理实例的调用处理程序 实现的接口.
每个代理实例都具有一个关联的调用处理程序.对代理实例调用方法时,将对方法调用进行编码并将其指派到它的调用处理程序的 invoke 方法.
invoke(Object proxy, Method method, Object[] args)参数解析:
proxy- 调用该方法的代理实例
method-MethodInstance corresponding to the interface method invoked on the proxy instance.The declared class of the objectMethodwill be the interface declaring the method,It may be a superinterface of the proxy interface from which the proxy class inherits methods.
args- 包含在代理实例上的方法调用中传递的参数值的对象数组,或者nullIf the interface method does not accept any parameters.Parameters of primitive types are wrapped in an instance of the appropriate primitive wrapper class,例如 java.lang.Integeror java.lang.Boolean.
ProxyCreate a static proxy class
Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
new Class<?>[] { Foo.class },
handler);参数解析:
loader- 定义代理类的类加载器
interfaces- 代理类要实现的接口列表
h- 将方法调用分派到的调用处理程序
学习案例1:
Rent接口
//租房接口
public interface Rent {
public void rent(); //租房
}
Host房东类
//The landlord implements the rental interface,It means he wants to rent a house
public class Host implements Rent {
@Override
public void rent() {
System.out.println("The landlord needs to rent out the house!");
}
}
ProxyInvocationHandler 处理程序类
//等我们会用这个类,自动生成代理类
public class ProxyInvocationHandler implements InvocationHandler {
//创建被代理的接口
private Rent rent;
public void setRent(Rent rent){
this.rent = rent;
}
//生成得到的代理类
public Object getProxy(){
//Create a proxy class
return Proxy.newProxyInstance(this.getClass().getClassLoader(), rent.getClass().getInterfaces(),this);
}
//处理代理实例,并返回结果,When the implementation interface is,系统会默认执行invoke处理程序
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//Executive landlord rents out the house
Object result = method.invoke(rent, args);
seeHouse();
SignContract();
return result;
}
//处理其他业务
public void seeHouse(){
System.out.println("中介带看房子");
}
//处理其他业务
public void SignContract(){
System.out.println("中介签合同");
}
}主类
public class Client {
public static void main(String[] args) {
//The real character is the landlord,He needs to find an agent to rent her a house
Host host = new Host();
//There is no proxy role for the time being,So we need to create
ProxyInvocationHandler pih = new ProxyInvocationHandler();
pih.setRent(host); //Setting up a landlord requires an agent
//A proxy class for a landlord to rent a house has been created.
Rent proxy = (Rent) pih.getProxy();
//Executive rental
proxy.rent();
}
}学习案例2 -- Universal dynamic proxy:
UserService接口
public interface UserService {
void add();
void update();
void delete();
void select();
}
UserServiceImpl实现类:
public class UserServiceImpl implements UserService{
@Override
public void add() {
System.out.println("使用了add方法 ");
}
@Override
public void update() {
System.out.println("使用了update方法 ");
}
@Override
public void delete() {
System.out.println("使用了delete方法 ");
}
@Override
public void select() {
System.out.println("使用了select方法 ");
}
}ProxyInvocationHandler类:
//Create a universal dynamic proxy
//等我们会用这个类,自动生成代理类
public class ProxyInvocationHandler implements InvocationHandler {
//创建被代理的接口
private Object target;
public void setTarget(Object target) {
this.target = target;
}
//生成得到的代理类
public Object getProxy() {
//Create a proxy class
return Proxy.newProxyInstance(this.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
}
//处理代理实例,并返回结果,When the implementation interface is,系统会默认执行invoke处理程序
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//Dynamically get the name of the method
System.out.println("debug提示:"+"使用了"+method.getName()+"方法");
//Executive landlord rents out the house
Object result = method.invoke(target, args);
return result;
}
}
主类:
public class Client {
public static void main(String[] args) {
//创建一个真实角色
UserService userService = new UserServiceImpl();
//Generate a proxy stunning
ProxyInvocationHandler pih = new ProxyInvocationHandler();
pih.setTarget(userService);
//创建代理类
UserService proxy = (UserService) pih.getProxy();
proxy.delete();//执行删除方法
}
}边栏推荐
- DDR4内存条电路设计
- matplotlib
- Unity底层是如何处理C#的
- 国密规范 SM2 SM3 SM4
- 项目2-年收入判断
- Activity的四种状态
- Redis source code: how to view the Redis source code, the order of viewing the Redis source code, the sequence of the source code from the external data structure of Redis to the internal data structu
- Waldom Electronics宣布成立顾问委员会
- 【软件测试】(北京)字节跳动科技有限公司终面HR面试题
- SQL sliding window
猜你喜欢
随机推荐
3.1-分类-概率生成模型
2022-08-09 Group 4 Self-cultivation class study notes (every day)
流式结构化数据计算语言的进化与新选择
JVM学习——3——数据一致性
1101 How many times B is A (15 points)
易观分析联合中小银行联盟发布海南数字经济指数,敬请期待!
Go语言实现Etcd服务发现(Etcd & Service Discovery & Go)
语音信号处理:预处理【预加重、分帧、加窗】
进制转换间的那点事
4.1-支持向量机
1081 检查密码 (15 分)
opencv实现数据增强(图片+标签)平移,翻转,缩放,旋转
Pinduoduo API interface
go-grpc TSL认证 解决 transport: authentication handshake failed: x509 certificate relies on ... ...
SQL滑动窗口
计算YUV文件的PSNR与SSIM
无服务器+域名也能搭建个人博客?真的,而且很快
囍楽云任务源码
详述MIMIC 的ICU患者检测时间信息表(十六)
基于FPGA的FIR滤波器的实现(4)— 串行结构FIR滤波器的FPGA代码实现








