当前位置:网站首页>动态代理
动态代理
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();
}
边栏推荐
- Is industrial securities a state-owned enterprise? Is it safe to open an account in industrial securities?
- Presto Web UI introduction
- 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
- Data Lake survey
- Under what circumstances will Flink combine operator chains to form operator chains?
- 网络 | traceroute,路由跟踪命令,用于确定 IP 数据包访问目标地址所经过的路径。
- 揭秘GaussDB(for Redis):全面对比Codis
- Multiple clicks of the button result in results
- Continue to cut the picture after the ArcGIS Server is disconnected
- 为什么ping不通网站 但是却可以访问该网站?
猜你喜欢
Niuke.com: host scheduling
How terrible is it not to use error handling in VFP?
云原生数据湖以存储、计算、数据管理等能力通过信通院评测认证
黑马畅购商城---8.微服务网关Gateway和Jwt令牌
Share 7 immortal wallpaper websites, let the new wallpaper give you a little joy, and don't fall into the repetition year after year.
The cloud native data lake has passed the evaluation and certification of the ICT Institute with its storage, computing, data management and other capabilities
Database Series: MySQL index optimization summary (comprehensive version)
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
Translation of meisai C topic in 2022 + sharing of ideas
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%
随机推荐
揭秘GaussDB(for Redis):全面对比Codis
2022 mathematical modeling competition time and registration fee
ROS 笔记(06)— 话题消息的定义和使用
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
JVM shutdown hook details
RPC typical framework
Thingspanel releases Internet of things mobile client (multiple pictures)
一個硬件工程師走過的彎路
Specific meanings of node and edge in Flink graph
Translation of meisai C topic in 2022 + sharing of ideas
Nacos installation and use
SQL injection vulnerability (type chapter)
Spark runs wordcount (case 1)
.Net Core 中使用工厂模式
Simple use of stream
CFCA Anxin sign access
Uncover gaussdb (for redis): comprehensive comparison of CODIS
4 life distributions
客从何处来
Which securities company's account is better and safer to open