当前位置:网站首页>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();
}边栏推荐
- confluence7.4. X upgrade record
- 现在网上炒股开户身份证信息安全吗?
- Linear regression of common mathematical modeling models for College Students
- 15、wpf之button样式小记
- RecyclerView滚动到指定位置
- PD1.4转HDMI2.0转接线拆解。
- How do super rookies get started with data analysis?
- The idea of mass distribution of GIS projects
- 20、wpf之MVVM命令绑定
- The latest IT learning route in 2020
猜你喜欢

Dark horse shopping mall ---6 Brand, specification statistics, condition filtering, paging sorting, highlighting

What is Flink? What can Flink do?

黑馬暢購商城---3.商品管理

An easy-to-use seal design tool - (can be converted to ofd file)

Architects reveal the difference between working in Alibaba, Tencent and meituan

一款好用的印章设计工具 --(可转为ofd文件)

confluence7.4. X upgrade record

使用php脚本查看已开启的扩展

Explanation of ideas and sharing of pre-processing procedures for 2021 US game D (with pre-processing data code)

黑马畅购商城---1.项目介绍-环境搭建
随机推荐
Kotlin学习笔记
VFP a picture processing library, simple and easy to use, free of charge, worth recommending
[regression analysis] understand ridge regression with case teaching
. Using factory mode in net core
2020最新最全IT学习线路
Real software developers will use this method to predict the future
设置图片的透明度从左到右渐变
ARM V7 协处理器
一套自动化无纸办公系统(OA+审批流)源码:带数据字典
Explanation of ideas and sharing of pre-processing procedures for 2021 US game D (with pre-processing data code)
SDN系统方法 | 9. 接入网
The idea of mass distribution of GIS projects
VFP develops a official account to receive coupons, and users will jump to various target pages after registration, and a set of standard processes will be sent to you
2022年首期Techo Day腾讯技术开放日将于6月28日线上举办
开哪家证券公司的账户是比较好,比较安全的
R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、epiDisplay包的poisgof函数对拟合的泊松回归模型进行拟合优度检验(检验模型效果)
PD1.4转HDMI2.0转接线拆解。
VFP function to summarize all numeric columns of grid to cursor
flutter常用命令及问题
Old ou, a fox friend, has had a headache all day. The VFP format is always wrong when it is converted to JSON format. It is actually caused by disordered code