当前位置:网站首页>动态代理
动态代理
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
- 兴业证券是国企吗?在兴业证券开户资金安全吗?
- The idea of mass distribution of GIS projects
- The cloud native data lake has passed the evaluation and certification of the ICT Institute with its storage, computing, data management and other capabilities
- 为什么ping不通网站 但是却可以访问该网站?
- JS judge whether a number is in the set
- The temporary table from XML to VFP is simple and easy to use and worth collecting
- Network related encapsulation introduced by webrtc native M96 basic base module
- Sentinel integrated Nacos data source
- Thingpanel publie le client mobile IOT (Multi - images)
猜你喜欢

Source code analysis of AQS & reentrantlock

Application of analytic hierarchy process in college teaching evaluation system (principle + example + tool)

架构师为你揭秘在阿里、腾讯、美团工作的区别

Spark history server and event log details

揭秘GaussDB(for Redis):全面对比Codis

Niuke: rotation array

Why distributed IDS? What are the distributed ID generation schemes?

How terrible is it not to use error handling in VFP?

按钮多次点击造成结果

Flink deeply understands the graph generation process (source code interpretation)
随机推荐
What are redis avalanche, penetration and breakdown?
Sword finger offer II 091 Painting house: application of state machine DP
Two ways of redis persistence -- detailed explanation of RDB and AOF
Multiple clicks of the button result in results
What is the development history, specific uses and structure of the chip
分享7个神仙壁纸网站,让新的壁纸,给自己小小的雀跃,不陷入年年日日的重复。
Convergence by probability
How to use the markdown editor
A detour taken by a hardware engineer
Startups must survive
RecyclerView滚动到指定位置
cnds
Detailed explanation of spark specification
Thingpanel publie le client mobile IOT (Multi - images)
Golden sun education listed in the U.S.: a small cap medium cap stock with a market value of USD 360million
Uncover gaussdb (for redis): comprehensive comparison of CODIS
Develop two modes of BS mode verification code with VFP to make your website more secure
4 life distributions
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
Encapsulation of practical methods introduced by webrtc native M96 basic base module (MD5, Base64, time, random number)