当前位置:网站首页>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();//执行删除方法
}
}
边栏推荐
猜你喜欢
随机推荐
1002 Write the number (20 points)
1.2-误差来源
【sdx62】XBL设置共享内存变量,然后内核层获取变量实现
1071 小赌怡情 (15 分)
LeetCode brushing series -- 46. Full arrangement
prometheus学习5altermanager
When MySQL uses GROUP BY to group the query, the SELECT query field contains non-grouping fields
Unity开发者必备的C#脚本技巧
1003 I want to pass (20 points)
流式结构化数据计算语言的进化与新选择
常见激活函数及其导数
[Recommender System]: Overview of Collaborative Filtering and Content-Based Filtering
Edge 提供了标签分组功能
关于Excel实现分组求和最全文档
动态代理学习
CIKM 2022 AnalytiCup Competition: 联邦异质任务学习
TF通过feature与label生成(特征,标签)集合,tf.data.Dataset.from_tensor_slices
【Pytorch】nn.Linear,nn.Conv
Pinduoduo API interface
易观分析联合中小银行联盟发布海南数字经济指数,敬请期待!