当前位置:网站首页>Dynamic proxy
Dynamic proxy
2022-06-25 12:16:00 【User 9854323】
Application scenario of agent mode :
1、 For example, to add monitoring to a method , Record the time when the method started , Time at the end of the method .
Static proxy :
Disadvantages of static proxy : Interface and proxy classes are 1 Yes 1 Of , There are multiple interfaces that need proxies , You need to create multiple proxy classes , tedious , Class blast .
public interface IPerson {
void say();
}
public static class Man implements IPerson{
@Override
public void say() {
}
}
/**
* Disadvantages of static proxy : Interface and proxy classes are 1 Yes 1 Of , There are multiple interfaces that need proxies , You need to create multiple proxy classes , tedious , Class blast .
* So there is a dynamic proxy
*/
public class ManProxy implements IPerson{
private IPerson target;
public IPerson getTarget() {
return target;
}
public ManProxy setTarget(IPerson target) {
this.target = target;
return this;
}
@Override
public void say() {
if (target != null) {
// Such as monitoring say Method start time
target.say();
// Such as monitoring say End time of method
}
}
}
A dynamic proxy :
/**
* A dynamic proxy
*/
public static class NormalHandler implements InvocationHandler {
private Object target;
public NormalHandler(Object target) {
this.target = target;
}
// The first parameter is the proxy object , The second parameter is the called method object , The third method is the call parameter .
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// Such as monitoring target Method start time
method.invoke(target, args);
// Such as monitoring target End time of method
return null;
}
}
/**
* Call of dynamic proxy
*/
public static void main(String[] args) {
Man man = new Man();
IPerson iPerson = (IPerson) Proxy.newProxyInstance(
man.getClass().getClassLoader(), //ClassLoader
man.getClass().getInterfaces(), //interfaces
new NormalHandler(man)); //InvocationHandler
iPerson.say();
}
边栏推荐
- R语言dplyr包filter函数过滤dataframe数据中指定数据列的内容不是(不等于指定向量中的其中一个)指定列表中的数据行
- 【OceanBase】OceanBase简介及其与MySQL的比较
- How do super rookies get started with data analysis?
- [论]Learning Dynamic and Hierarchical Traffic Spatiotemporal Features with Transformer
- 学习笔记 2022 综述 | 自动图机器学习,阐述 AGML 方法、库与方向
- 文献之有效阅读
- R语言使用构建有序多分类逻辑回归模型、epiDisplay包的ordinal.or.display函数获取有序logistic回归模型的汇总统计信息(变量对应的优势比及其置信区间、以及假设检验的p值)
- Explain factor analysis in simple terms, with case teaching (full)
- 【数据中台】数据中台的OneID是个什么鬼,主数据它不香吗?
- plt. GCA () picture frame and label
猜你喜欢
How far is it from the DBF of VFP to the web salary query system?
Develop two modes of BS mode verification code with VFP to make your website more secure
Architects reveal the difference between working in Alibaba, Tencent and meituan
揭秘GaussDB(for Redis):全面對比Codis
Dark horse shopping mall ---8 Microservice gateway and JWT token
数据库系列:MySQL索引优化总结(综合版)
Oracle Spatial creating spatial tables
15、wpf之button样式小记
Using DBF of VFP to web salary query system
flutter常用命令及问题
随机推荐
R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用epiDisplay包的lrtest函数执行多个模型似然比检验对比两个模型的性能是否有差异
图片打标签之获取图片在ImageView中的坐标
Network related encapsulation introduced by webrtc native M96 basic base module
Web project development process
【OceanBase】OceanBase简介及其与MySQL的比较
Develop two modes of BS mode verification code with VFP to make your website more secure
apple 为什么要改 objc_msgSend 的类型申明
Real software developers will use this method to predict the future
属性分解 GAN 复现 实现可控人物图像合成
为什么ping不通网站 但是却可以访问该网站?
Implementing Domain Driven Design - using the ABP framework - Summary of a series of articles
ARM 立即数
Arm immediate
动态代理
VFP serial port communication is difficult for 9527. Maomao just showed his skill and was defeated by kiss
ROS notes (06) - definition and use of topic messages
RecyclerView滚动到指定位置
Découvrir gaussdb (pour redis): une comparaison complète avec Codis
黑马畅购商城---8.微服务网关Gateway和Jwt令牌
VFP uses Kodak controls to control the scanner to solve the problem that the volume of exported files is too large