Static proxy and dynamic proxy

Source: Internet
Author: User

Static proxy is a type of proxy class during compilation. Its. Class file is produced before running. You can use a static proxy class to process and filter messages before the delegate class completes a specified call.

A simple example is as follows:

 

Package search; public class helloserviceproxy {private helloservice; Public helloserviceproxy (helloservice) {This. helloservice = helloservice;} Public String echo (string MSG) {system. out. println ("preprocessing"); string result = helloservice. echo (MSG); system. out. println ("post-event processing"); return result ;}}
package search;public class Server {public static void main(String[]args){HelloService service=new HelloServiceImpl();HelloServiceProxy proxy=new HelloServiceProxy(service);System.out.println(proxy.echo("hello"));}}

Call the echo method of the helloserviceproxy class to call the helloservice class, and process the message before the call.

 

The running result is as follows:

Preprocessing
Post-event processing
Echo hello

The dynamic proxy class dynamically generates the proxy class at runtime through the reflection mechanism.

The proxy and invocationhandler in the Java. Lang. Reflect package are required to implement the proxy class.

The proxy class provides static methods for creating dynamic proxy classes and their instances.

Public static class <?> Getproxyclass (classloader loader, class <?>... Interfaces)

Loader specifies the class loader of the proxy class, and interfaces specifies all interfaces to be implemented by the proxy class.

Public static object newproxyinstance (classloader loader, class <?> [] Interfaces, invocationhandler H)

Loader specifies the class loader of the proxy class, interfaces specifies all interfaces to be implemented by the proxy class, and H specifies the invocationhandler object associated with the proxy class.

The dynamic proxy class created by the static method of the proxy class has the following features:

1. The dynamic proxy class is public, final, and non-abstract;

2. The dynamic proxy class inherits the proxy class;

3. The name of the dynamic proxy class starts with "$ proxy;

4. The dynamic proxy class implements all interfaces specified by interfaces in the getproxyclass () and newproxyinstance () methods;

5. The isproxyclass () Static Method of the proxy class can be used to determine whether the class specified by the parameter is a dynamic proxy class;

6. The dynamic proxy class has a public constructor. The constructor has an invocationhandler parameter;

Dynamic proxy instances created by static proxy methods have the following features:

1. Each instance is associated with the invocationhandler instance.

2. When the program calls the XX method of the dynamic proxy instance, this method will call the invoke () method of the invocationhandler object associated with it.

An example of a simple dynamic proxy class is as follows:

 

Package search; import Java. lang. reflect. invocationhandler; import Java. lang. reflect. method; import Java. lang. reflect. proxy; public class helloserviceproxyfactory {public static helloservice gethelloserviceproxy (final helloservice) {invocationhandler handler = new invocationhandler () {public object invoke (Object proxy, method, object [] ARGs) throws throwable {system. out. println ("preprocessing"); object result = method. invoke (helloservice, argS); system. out. println ("post-event processing"); return result ;}}; class classtype = helloservice. class; Return (helloservice) proxy. newproxyinstance (classtype. getclassloader (), new class [] {classtype}, Handler );}}

 

package search;public class Client {public static void main(String[]args){HelloService helloService=new HelloServiceImpl();HelloService proxy=HelloServiceProxyFactory.getHelloServiceProxy(helloService);System.out.println(proxy.getClass().getName());System.out.println(proxy.echo("hello"));}}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.