Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the reason why JDK dynamic agents must target interfaces?

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what is the reason why the JDK dynamic agent must aim at the interface". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

A dynamic agent is actually a program running

Dynamically generate the class file of the proxy class according to the interface of the agent

And load the process of running the class file

Found by decompiling the generated $Proxy0.class file

Public final class $Proxy0 extends Proxy implements Interface {public $Proxy0 (InvocationHandler paramInvocationHandler) {super (paramInvocationHandler);} / / this method is the business method of the proxied interface, and the proxy class automatically generates the corresponding method, which executes the invoke method of invocationHandler. Public final void sayHello (String paramString) {try {this.h.invoke (this, m3, new Object [] {paramString}); return;} catch (Error | RuntimeException localError) {throw localError;} catch (Throwable localThrowable) {throw new UndeclaredThrowableException (localThrowable);}

Java is a single inheritance

The dynamically generated proxy class already inherits the Proxy class, so it can no longer inherit other classes

Therefore, it can only be implemented in the form of the interface of the proxied class, so the dynamic proxy of JDK must have an interface.

Why does calling a method of a proxy class automatically enter the invoke () method of InvocationHandler?

Because the invoke method uses jdk reflection to call the real business method of the proxied class

And we can also add some custom logic before and after the method.

This is the end of the content of "what is the reason why the JDK dynamic agent must target the interface". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report