Proxy implements java Dynamic Proxy

Source: Internet
Author: User

In the java design mode, the proxy mode is widely used. For example, when I write a web program and modify the request or response in the filter, the request does not have a set method, in this way, you need to use a new class to implement the HttpServletRequest interface, put the original request into the new class and call the original method for the corresponding method in the new class. Then, the server passes the request to the Servlet so that the request is not the original request, but the request I have made., in this way, you can modify the value of the original class and then return it when calling the processed request value. However, there are many methods to implement HttpServletRequest, which is also difficult for us to use dynamic proxy. The following lists the classes or interfaces used to dynamically generate a proxy. Public interface: the interface that must be implemented by both the proxy class and the handler. You do not need to implement it when using a dynamic proxy to define the proxy class. You can specify it when running the program. publicInterface // custom public interface 1 package com. lw. proxy; 2 3 public interface PublicInterface {4 5 public void defaultMethod (); 6} handler: handler is a class that implements business processing, the proxy class calls the handler method to complete the corresponding business (this value is returned if the Handler has a return value), so that changes can be made before or after the handler method is called. the handler must implement the InvocationHandler interface and PublicInterface (public interface). Note: The implementation of the invoke method package com. lw. proxy; import java. lang. reflect. invocationHandler; import Java. lang. reflect. method; public class HandlerClass implements PublicInterface, InvocationHandler {@ Override public void defaultMethod () {// TODO Auto-generated method stub System. out. println ("handler method Processing Service");}/*** implement this method. When calling this method, call the method parameter on the handler object. */public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {return method. invoke (this, args) ;}} proxy class: the proxy class is the implementation class of a series of real interfaces. Because it is a dynamic proxy class, no interface is implemented during definition. Note: You must have a constructor that receives the Invocationhandler interface. package com. lw. proxy; import java. lang. reflect. invocationHandler; public class ProxyClass {private InvocationHandler handler; public ProxyClass (InvocationHandler handler) {super (); this. handler = handler;} java. lang. reflect. the Proxy class will generate a dynamic Proxy class. Proxy static method: // specifies the Class loader of the Proxy Class and the interface to be implemented by the Proxy Class. This method returns the Class public static Class that implements all the specified interfaces. <?> GetProxyClass (ClassLoader loader, Class <?>... Interfaces) // use this method to generate an instance of the proxy class. Public static Object newProxyInstance (ClassLoader loader, Class <?> [] Interfaces, InvocationHandler h) Main Program: <span style = "font-size: 14px;"> package com. lw. proxy; import java. lang. reflect. constructor; import java. lang. reflect. invocationHandler; import java. lang. reflect. proxy; public class TestProxy {public static void main (String [] args) throws Exception {/*** generate dynamic Proxy class ** specifies the class loader of the dynamic Proxy class, and the interface to be implemented. * Returns the class that implements all specified interfaces. */Class <?> Cl = Proxy. getProxyClass (ProxyClass. class. getClassLoader (), PublicInterface. class); // obtain the Constructor that receives the InvocationHandler parameter. <?> Con = cl. getConstructor (InvocationHandler. class); // use con to build an instance. This implementation will implement all the specified interfaces PublicInterface proxyClass = (PublicInterface) con. newInstance (new HandlerClass (); // call the proxy method proxyClass. defaultMethod ();/*** second method for constructing a dynamic proxy Class */Class [] cls = new Class [] {PublicInterface. class}; ClassLoader loader = ProxyClass. class. getClassLoader (); InvocationHandler h = new HandlerClass (); proxyClass = (PublicInterface) Proxy. newProxyInstance (loader, cls, h); // call proxyClass. defaultMethod () ;}</span>

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.