JDK dynamic proxy and proxy simple demonstration of dynamic CGLib

After JDK1.3, Java technology provides a dynamic proxy, proxy instance allows developers to create interfaces during operation.

I. First, we demonstrate JDK dynamic proxies.

Now we have a simple business interface Saying, as follows:

package testAOP;
public interface Saying {
public void sayHello(String name);    
public void talking(String name);
}

A simple implementation class SayingImpl, as follows:

package testAOP;
public class SayingImpl implements Saying {
    @Override
    public void sayHello(String name) {
        // TODO Auto-generated method stub
        System.out.println(name + ":大家好啊!");
    }
    @Override
    public  void at Talking (String name) {
         // TODO Auto-Generated Stub Method, 
        System.out.println (name + ":! I mean, we should strive to build a harmonious society" );
    }
}

We want to achieve that, and talking before and after each sayHello dynamic implant treatment.

JDK dynamic proxy class uses two main java.lang.reflect package: Proxy and InvocationHandler.

InvocationHandler is an interface defined by transverse logic implementing this interface, the calling code and the target class by reflection, dynamic crosscutting and business logic braided together.

Proxy use InvocationHandler dynamically create an instance of a line with an interface to generate proxy object target class.

Below, we create a InvocationHandler instance:

package testAOP;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class MyInvocationHandler implements InvocationHandler {

    private Object target;
    MyInvocationHandler(Object target){
        this.target = target;
    }
    @Override
    public Object Invoke (Object Proxy, Method, Method, Object [] args)
             throws the Throwable {
         // the target before performing the method 
        System.out.println ( "------------------- ------- " );
        System.out.println ( "The next stage please speak!" );
         // target method is called 
        Object obj = Method.invoke (target, args);
         // execute the target method 
        System.out.println ( "everyone a round of applause ! " );
         return obj;
    }
}

Here is the test:

package testAOP;

import java.lang.reflect.Proxy;

public class JDKProxyTest {
    
    public  static  void main (String [] args) {
         // desired target service agent is based 
        Saying target = new new SayingImpl ();
         // braided target class and the class with transverse 
        MyInvocationHandler Handler = new new MyInvocationHandler (target);
         / / create a proxy instance 
        Saying proxy = (Saying) Proxy.newProxyInstance (
                . target.getClass () getClassLoader (), // class loader object class 
                target.getClass () The getInterfaces (),. // target class Interface 
                Handler); // transverse class 
        proxy.sayHello ( "Bob" ) ;
        proxy.talking ( 'Mary' );
    }
}

Operation is as follows:

——————————————————————————
Please speak next stage!
Xiao Ming: you good!
Everyone a round of applause!
——————————————————————————
Please speak next stage!
Mary: I mean, we should strive to build a harmonious society!
Everyone a round of applause!

Use JDK dynamic proxies have one big limitation is that it requires that the target class must implement the interface of the corresponding method, it can only create a proxy instance for the interface. We can see in the above test method newProxyInstance Proxy class of the method the second parameter is the target class interface. If the class does not implement the interface, which rely on dynamic cglib agents.

CGLib with very bottom of the byte code technology, to create a subclass of a class, and the method call interception techniques to intercept all the parent class method in the subclass, and homeopathic implant transverse logic.

 

Second, the next we were cglib dynamic proxy presentation.

First, we need to guide the package, I use the package is cglib-nodep-2.1_3.jar.

We first create a proxy creator CglibProxy:

package testAOP.cglib;

import java.lang.reflect.Method;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class CglibProxy implements MethodInterceptor{

    Enhancer Enhancer = new new Enhancer ();
     public Object The getProxy (Class clazz) {
         // subclasses need to create provided 
        enhancer.setSuperclass (clazz);
        enhancer.setCallback ( the this );
         // dynamically created instance of a subclass bytecode technique 
        return enhancer.create ();
    }
    @Override
    public Object intercept(Object obj, Method method, Object[] args,
            MethodProxy proxy) throws Throwable {
        // TODO Auto-generated method stub
        System.out.println("——————————————————————————");
        System.out.println ( "The next stage please speak!" );
         // target method is called 
        Object the Result = proxy.invokeSuper (obj, args);
         // execute the target method 
        System.out.println ( "everyone a round of applause ! " );
         return the Result;
    }    
}

Then tested:

package testAOP.cglib;

import testAOP.Saying;
import testAOP.SayingImpl;

public class CglibProxyTest {

    public static void main(String[] args) {
        Proxy CglibProxy = new new CglibProxy ();
         // create a proxy class dynamically generated by the subclass manner 
        Saying target = (Saying) proxy.getProxy (SayingImpl. Class );
        target.sayHello("小明");
        target.talking("小丽");
    }
}

The results did not make any difference with the JDK dynamic proxy.

CGLib dynamic proxy classes and interfaces can act, but not final class of agents, but also has some limitations.

JDK dynamic proxy agent and CGLib dynamic runtime are enhanced by cross proxy class code into enhanced mode. And this difference is that AspectJ, it can be implanted enhanced by a special compiler crosscutting code at compile time, this enhancement processing advantage in running time, since JDK dynamic proxies and CGLib dynamic proxy need each run enhancement.

Reproduced in: https: //www.cnblogs.com/zrtqsk/p/3600588.html

Guess you like

Origin blog.csdn.net/weixin_33923148/article/details/93248507