Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

The method of dynamically generating proxy Class by java

Shulou Source: shulou.com Published: 2022-05-31 10:52:42 10月04日 Update

Most people do not understand the knowledge points of this "java dynamic generation proxy class method" article, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "java dynamic generation proxy class method" article.

Description

1. Call the newProxyInstance method of the Proxy class to get the proxy class instance.

2. This proxy class implements the specified interface and distributes the method calls to the specified call handler.

Method declaration

Public static Object newProxyInstance (ClassLoader loader, Class [] interfaces, InvocationHandler h) throws IllegalArgumentException

Parameters.

Loader: defines the ClassLoder of the proxy class

Interfaces: list of interfaces implemented by the proxy class

H: call the handler, which is the instance of the class we defined above that implements the InvocationHandler interface

Example

Public class Main {public static void main (String [] args) {/ / create a mediation class instance DynamicProxy inter = new DynamicProxy (new Vendor ()); / / add this sentence to generate a $Proxy0.class file, which is the dynamically generated proxy class file System.getProperties (). Put ("sun.misc.ProxyGenerator.saveGeneratedFiles", "true") / / get the proxy class instance sell Sell sell = (Sell) (Proxy.newProxyInstance (Sell.class.getClassLoader (), new Class [] {Sell.class}, inter)); / / call the proxy class method through the proxy class object, which will actually go to the invoke method call sell.sell (); sell.ad () }} the above is the content of this article on "the method of dynamically generating proxy classes in java". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.

Tags: Agents methods content instances dynamic generation interfaces files processors articles knowledge articles processing intermediation that is values parameters most actually actually Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft vpn Shulou Technology Shulou Information NVidia