当前位置:网站首页>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();//执行删除方法
}
}
边栏推荐
猜你喜欢
Implementation of FIR filter based on FPGA (5) - FPGA code implementation of parallel structure FIR filter
Redis source code-String: Redis String command, Redis String storage principle, three encoding types of Redis string, Redis String SDS source code analysis, Redis String application scenarios
从何跟踪伦敦金最新行情走势?
easyrecovery15数据恢复软件收费吗?功能强大吗?
1106 2019数列 (15 分)
常见激活函数及其导数
1101 B是A的多少倍 (15 分)
易观分析联合中小银行联盟发布海南数字经济指数,敬请期待!
【软件测试】(北京)字节跳动科技有限公司终面HR面试题
prometheus学习4Grafana监控mysql&blackbox了解
随机推荐
关于Android Service服务的面试题
Redis source code-String: Redis String command, Redis String storage principle, three encoding types of Redis string, Redis String SDS source code analysis, Redis String application scenarios
Depth (relay supervision)
1036 跟奥巴马一起编程 (15 分)
3.1-分类-概率生成模型
恒源云-Pycharm远程训练避坑指南
oracle19c不支持实时同步参数,请教一下大佬们有什么好的解决办法吗?
一张图了解JVM八大原子操作
Activity的四种启动模式
1101 How many times B is A (15 points)
MindManager2022全新正式免费思维导图更新
1002 Write the number (20 points)
js判断图片是否存在
Douyin API interface
Unity游戏排行榜的制作与优化
2022年中国软饮料市场洞察
pytorch,numpy两种方法实现nms类间+类内
Redis测试
【LeetCode每日一题】——844.比较含退格的字符串
break pad源码编译--参考大佬博客的总结