java--Dynamic Agent

Source: Internet
Author: User



In static proxies, when we call the target class, we get the proxy class first. Because the proxy class takes the target class as a member variable and inherits the same interface as the target class, it has the same method, so in the proxy class. Special operations are inserted before and after the target class method is run by calling the method of the destination class.


However, static agents have two drawbacks: 1, the proxy class is particularly many, each add a target class. Will write an agent. 2, in the proxy class, we may need to insert the same operation. This causes the code to be repeated.


so. By introducing reflection, dynamic proxies can be implemented to improve static proxies.



/** * @ClassName: Loghandler * @Description: Used to create proxy class * @author paddy field * @date December 8, 2015 morning 11:17:51 */public class Loghandler    Implements Invocationhandler {private Object targetObject; public Object Newproxyinstance (object targetobj) {this.targetobject = targetobj;/* * 1, gets the class's ClassLoader * 2, gets the interface of the class created * 3, passed The callback invoke method is located in the class * */return proxy.newproxyinstance (Targetobj.getclass (). getClassLoader (), Targetobj.getclass ().    Getinterfaces (), this);     }/* * (non-Javadoc) <p>Title:invoke</p> <p>description: </p> * * @param proxy * * @param Call method information * * @param args: Array of parameters of the method * * @return * * @throws throwable * * @ See Java.lang.reflect.invocationhandler#invoke (Java.lang.Object, * java.lang.reflect.Method, java.lang.object[]) * /@Override public object Invoke (object proxy, Method method, object[] args) throws Throwable {for (int i = 0; i < Args.length; i++) {System.out.println (args[i]);} try {//Call method of the target class, return an object, if the method does not return a value, then return null method.invoke (TargetObject, args);}    catch (Exception e) {e.printstacktrace (); throw e;}    System.out.println ("Start----->>>" + method.getname ()); return Method.getname (); }}

Use the Demo sample:


/* Dynamic Proxy Demo sample */loghandler Loghandler = new Loghandler (); Usermanager Usermanager = (usermanager) loghandler.newproxyinstance (New Usermanagerimpl ()); UserManager.addUser (" 2343254 "," LHC ");


We put the action to be inserted before and after the called method in the target class is placed in the callback function invoke method, when the corresponding method is invoked using the proxy class. Will go directly to the Invoke method here. Use method, and invoke the third parameter to get the information of the called methods.


after the dynamic agent has been added. The two minor drawbacks of static proxies are obviously removed.





java--Dynamic Agent

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.