Two implementation methods of Java Dynamic agent are explained in detail
The dynamic proxy of Java is described in the API connecting to Java, which is not written here. I understand the agent:
Extending the functions of a specific method in a specific interface is called a proxy. A proxy invokes a method through a call handler object associated with a proxy instance.
Here is an example:
Interface:
Implementation class:
Let's take a look at how the invoke method in method is described in the API
That is, the calling handler calls the underlying method represented by the method object on the implementation class object of the interface.
The first way to implement an agent:
The second way to implement the proxy is to implement the invocationhandler interface
Test code:
Console results:
The second method is a little confused. I don't know if you have it. That is, you call the invoke method in numproxyimpl that is not displayed, but it is executed. Uh huh, go down and have a look
If you don't want trouble, just remember.
For example, agents can be used for coding. Write an example next time.
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.