A small example of a Java proxy

Source: Internet
Author: User
Tags comparable

Proxies allow you to create a new class at run time that implements a given set of interfaces.

A call processor (invocation handler) is required, and the calling processor needs to implement the Invocationhandler interface. Whenever a method of a proxy object is invoked, the Invoke method of the calling processor is called and passed to it with the methods object and the original invocation parameters.

To create a proxy object, you need to use the Newproxyinstance method of the proxy class. This method has three parameters:

1, a ClassLoader (class loader), NULL stands for the default

2. Interface array

3. One Call processor

The role of using proxies:

1. Route method calls to the remote server

2. Associate user interface events with actions during program run (Spring AOP is)

3, for debugging, tracking method calls

The following example is a trace method call. Take the binary lookup method as an example, note: integer implements the comparable interface, the proxy class overrides the ToString method:

Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;ImportJava.lang.reflect.Field;ImportJava.lang.reflect.InvocationHandler;Importjava.lang.reflect.InvocationTargetException;ImportJava.lang.reflect.Method;ImportJava.lang.reflect.Proxy;Importjava.util.Arrays;Importjava.util.Date;ImportJava.util.Random;ImportJavax.swing.JOptionPane;ImportJavax.swing.Timer; Public classTextmain { Public Static voidMain (String args[]) {object[] elements=Newobject[1000];  for(inti=0;i<elements.length;i++) {Integer value=i+1; Invocationhandler Handler=NewTracehandler (value); Object Proxy=proxy.newproxyinstance (NULL,NewClass[]{comparable.class}, handler); Elements[i]=proxy; } Integer Key=NewRandom (). Nextint (elements.length) +1; intresult=Arrays.binarysearch (elements, key); if(result>=0) System.out.println (Elements[result]);}}classTracehandlerImplementsinvocationhandler{PrivateObject Target;  PublicTracehandler (Object t) {target=T; } @Override Publicobject Invoke (Object proxy, Method method, object[] args)throwsThrowable {//TODO auto-generated Method StubSystem.out.print (target); System.out.print ("." +method.getname () + "("); if(args!=NULL){             for(inti=0;i<args.length;i++) {System.out.print (args[i]); if(i<args.length-1) System.out.print (","); }} System.out.println (")"); returnMethod.invoke (target, args); }    }

Operation Result:

500.compareTo (269) 250.compareTo (269) 375.compareTo (269) 312.compareTo (269) 281.compareTo (269) 265.compareTo (269) 273.compareTo (269) 269.compareTo (269) 269.toString () 269

The Invoke method in Invocationhandler, which defines the actions that you want to perform when the proxy object invokes the method (trace, log logging)

The newproxyinstance constructs an instance of the proxy class that implements the specified interface, and all methods invoke the Invoke method of the given processor object.

All proxy classes are extended to the proxy class, Proxy has only one instance domain--call processor Invocationhandler

A small example of a Java proxy

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.