当前位置:网站首页>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
-Method
Instance corresponding to the interface method invoked on the proxy instance.The declared class of the objectMethod
will be the interface declaring the method,It may be a superinterface of the proxy interface from which the proxy class inherits methods.
args
- 包含在代理实例上的方法调用中传递的参数值的对象数组,或者null
If 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.Integer
or 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();//执行删除方法
}
}
边栏推荐
- tf.cast(),reduce_min(),reduce_max()
- 1076 Wifi密码 (15 分)
- js判断图片是否存在
- 【latex异常和错误】Missing $ inserted.<inserted text>You can‘t use \spacefactor in math mode.输出文本要注意特殊字符的转义
- 年薪40W测试工程师成长之路,你在哪个阶段?
- break pad源码编译--参考大佬博客的总结
- NTT的Another Me技术助力创造歌舞伎演员中村狮童的数字孪生体,将在 “Cho Kabuki 2022 Powered by NTT”舞台剧中首次亮相
- 博途PLC 1200/1500PLC ModbusTcp通信梯形图优化汇总(多服务器多从站轮询)
- 6月各手机银行活跃用户较快增长,创半年新高
- 联想集团:2022/23财年第一季度业绩
猜你喜欢
随机推荐
动态代理学习
Pytorch模型转ONNX模型
伦敦银规则有哪些?
easyrecovery15数据恢复软件收费吗?功能强大吗?
Tf中的平方,多次方,开方计算
How Unity programmers can improve their abilities
1106 2019 Sequence (15 points)
1106 2019数列 (15 分)
3.2-分类-Logistic回归
【软件测试】(北京)字节跳动科技有限公司二面笔试题
prometheus学习4Grafana监控mysql&blackbox了解
Redis源码-String:Redis String命令、Redis String存储原理、Redis字符串三种编码类型、Redis String SDS源码解析、Redis String应用场景
测试用例很难?有手就行
What are the things that should be planned from the beginning when developing a project with Unity?How to avoid a huge pit in the later stage?
Unity游戏排行榜的制作与优化
【sdx62】XBL设置共享内存变量,然后内核层获取变量实现
1056 组合数的和 (15 分)
Discourse 的关闭主题(Close Topic )和重新开放主题
3GPP LTE/NR信道模型
1051 复数乘法 (15 分)