java-Dynamic Agent

Source: Internet
Author: User

There are 2 implementations of Java dynamic Agents, Jdkdynamicaopproxy and Cglib2aopproxy.

Spring's AOP is all used in this 2 implementation, JDK Dynamic agent is implemented by the internal reflection mechanism of Java, with Proxygenerator.generateproxyclass (... ) to generate the bytecode, the cglib dynamic proxy generates a subclass of the proxy class (so the final class is not dynamic proxy), and the underlying is implemented with ASM. In general, the reflection mechanism is more efficient in generating classes, and ASM is more efficient in the execution of the class after it is generated (you can cache the ASM-generated classes to solve the ASM-generated class-process inefficiencies). It is also important to note that the application premise of the JDK dynamic agent must be the target class based on the unified interface. The JDK dynamic agent cannot be applied without the above premise. It can be seen that the JDK dynamic agent has certain limitations, cglib this third-party class library Implementation of the dynamic proxy application more extensive, and more efficient advantages.

JDK Dynamic Agent

Analysis of the principle see: http://www.cnblogs.com/MOBIN/p/5597215.html

 package   Com.amigo.study.DynamicProxyDemo;  /**   * Created by Moi on 2017/10/1.  */ public  interface   Idbservice { public   String getdbname ();}  
 Package Com.amigo.study.DynamicProxyDemo; /**  */Publicclassimplements  idbservice    {    @Override  Public String Getdbname () {        System.out.println ("DB name is Amigo.") );         return "Amigo";    }}
 PackageCom.amigo.study.DynamicProxyDemo;ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method;/*** Created by Moi on 2017/10/1.*/ Public classDbserviceinvokehandlerImplementsInvocationhandler {PrivateObject Target; Dbserviceinvokehandler () {Super(); } dbserviceinvokehandler (Object target) {Super();  This. target =Target; }

/**
* The purpose of the dynamic proxy is often to enhance the proxy class, which is where the enhanced logic is placed.
* @param proxy
* @param method
* @param args
* @return
* @throws Throwable
*/

@Override PublicObject Invoke (Object proxy, Method method, object[] args)throwsThrowable {if("Getdbname". Equals (Method.getname ())) {System.out.println ("++++++before" + method.getname () + "++++++"); Object result=Method.invoke (target, args); System.out.println ("++++++after" + method.getname () + "++++++"); returnresult; } Else{Object result=Method.invoke (target, args); returnresult; }    }}
 PackageCom.amigo.study.DynamicProxyDemo;ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Proxy;/*** Created by Moi on 2017/10/1.*/ Public classDynamicproxytest { Public Static voidMain (string[] args) {//here is the. class file that was saved to be generated. Note: To create a new/com/sun/proxy directory in the project root directory, you will not get an error. System.getproperties (). Put ("Sun.misc.ProxyGenerator.saveGeneratedFiles", "true");    Jdkdynamicproxytest (); }     Public Static voidjdkdynamicproxytest () {Idbservice Idbservice=NewDbserviceimpl (); Invocationhandler Invocationhandler=NewDbserviceinvokehandler (Idbservice); Idbservice Userserviceproxy=(Idbservice) proxy.newproxyinstance (Idbservice.getclass (). getClassLoader (), Idbservice.getclass (). GetI        Nterfaces (), Invocationhandler);    System.out.println (Userserviceproxy.getdbname ()); }}
Cglib Dynamic Agent
 PackageCom.amigo.study.DynamicProxyDemo;ImportNet.sf.cglib.proxy.MethodInterceptor;ImportNet.sf.cglib.proxy.MethodProxy;ImportJava.lang.reflect.Method;/*** Created by Moi on 2017/10/1.*/ Public classDbservicemethodinterceptorImplementsMethodinterceptor {@Override PublicObject Intercept (Object o, Method, object[] objects, Methodproxy methodproxy)throwsthrowable {System.out.println ("Before:" +method); Object Object=Methodproxy.invokesuper (O, objects); System.out.println ("After:" +method); returnobject; }}
 PackageCom.amigo.study.DynamicProxyDemo;ImportNet.sf.cglib.proxy.Enhancer;ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Proxy;/*** Created by Moi on 2017/10/1.*/ Public classDynamicproxytest { Public Static voidMain (string[] args) {//jdkdynamicproxytest ();cglibdynamicproxytest (); }     Public Static voidjdkdynamicproxytest () {Idbservice Idbservice=NewDbserviceimpl (); Invocationhandler Invocationhandler=NewDbserviceinvokehandler (Idbservice); Idbservice Userserviceproxy=(Idbservice) proxy.newproxyinstance (Idbservice.getclass (). getClassLoader (), Idbservice.getclass (). GetI        Nterfaces (), Invocationhandler);    System.out.println (Userserviceproxy.getdbname ()); }     Public Static voidcglibdynamicproxytest () {Enhancer enhancer=NewEnhancer ();
Sets the proxy class Enhancer.setsuperclass (Dbserviceimpl.class);
The callback function is used to enhance the proxy class Enhancer.setcallback (Newdbservicemethodinterceptor ()); Dbserviceimpl Dbservice=(Dbserviceimpl) enhancer.create (); Dbservice.getdbname (); }}

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.