当前位置:网站首页>动态代理
动态代理
2022-06-25 11:49:00 【用户9854323】
代理模式的应用场景:
1、例如要给某个方法加上监控,记录方法开始时候的时间,方法结束时的时间。
静态代理:
静态代理缺点:接口与代理类是1对1的,有多个接口需要代理,就需要新建多个代理类,繁琐,类爆炸。
public interface IPerson {
void say();
}
public static class Man implements IPerson{
@Override
public void say() {
}
}
/**
* 静态代理缺点:接口与代理类是1对1的,有多个接口需要代理,就需要新建多个代理类,繁琐,类爆炸。
* 所以有了动态代理
*/
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) {
//例如监控say方法的开始时间
target.say();
//例如监控say方法的结束时间
}
}
}
动态代理:
/**
* 动态代理
*/
public static class NormalHandler implements InvocationHandler {
private Object target;
public NormalHandler(Object target) {
this.target = target;
}
//第一个参数是代理对象,第二个参数是被调用的方法对象,第三个方法是调用参数。
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//例如监控target方法的开始时间
method.invoke(target, args);
//例如监控target方法的结束时间
return null;
}
}
/**
* 动态代理的调用
*/
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();
}
边栏推荐
- 按钮多次点击造成结果
- Flink partition policy
- Mui scroll bar recovery
- Keywords serializable serialization and deserialization
- Solution to the timeout scenario of Flink streaming computing (official live broadcast)
- WebRTC Native M96 基础Base模块介绍之实用方法的封装(MD5、Base64、时间、随机数)
- JVM shutdown hook details
- Shichuang Energy sprint Technology Innovation Board: le chiffre d'affaires annuel prévu de 1,1 milliard de RMB est de 0,7 milliard de RMB, en baisse de 36%
- 为什么ping不通网站 但是却可以访问该网站?
- How terrible is it not to use error handling in VFP?
猜你喜欢
VFP uses Kodak controls to control the scanner to solve the problem that the volume of exported files is too large
時創能源沖刺科創板:擬募資11億 年營收7億淨利反降36%
Yisheng biological sprint scientific innovation board: 25% of the revenue comes from the sales of new crown products, and it is planned to raise 1.1 billion yuan
Idea local launch Flink task
Uncover gaussdb (for redis): comprehensive comparison of CODIS
Flink deeply understands the graph generation process (source code interpretation)
What is the development history, specific uses and structure of the chip
黑馬暢購商城---3.商品管理
Niuke: rotation array
Deeply understand Flink SQL execution process based on flink1.12
随机推荐
客从何处来
数据库系列:MySQL索引优化总结(综合版)
Startups must survive
Le détour d'un ingénieur en matériel
Sentinel integrated Nacos data source
一个硬件工程师走过的弯路
Detailed explanation of spark specification
现在网上炒股开户身份证信息安全吗?
How far is it from the DBF of VFP to the web salary query system?
redis的dict的扩容机制(rehash)
Niuke.com: host scheduling
What are redis avalanche, penetration and breakdown?
黑马畅购商城---1.项目介绍-环境搭建
RecyclerView滚动到指定位置
SMS verification before deleting JSP
Spark runs wordcount (case 1)
ROS 笔记(06)— 话题消息的定义和使用
Cesium editing faces
What is the development history, specific uses and structure of the chip
Using DBF of VFP to web salary query system