Analysis of Java Dynamic agent

Keywords: Programming Java network Spring

Java's dynamic Proxy mainly refers to the Proxy class under the java.lang.reflect package. The InvocationHandler interface under the same package will be used in the use process.

1. The proxy class provides a static method object newproxyinstance (classloader loader, class <? > [] interfaces, invocationhandler h) to generate proxy objects:

Loader is the class loader of the target class, interfaces is the interface implemented by the target class (not necessarily all interfaces implemented by it, just represented by class <? > []), and h is an object of the InvocationHandler type.

2. The invocationhandler interface provides a method Object invoke(Object proxy,Method method,Object[] args) throws Throwable to call the target method and provide new functions:

Proxy is the proxy object, method is the target method, and args is the parameter list of the target method.

The so-called dynamic agent is such an object: it is dynamically generated when the program is running. When generating it, you must provide a set of interfaces to it, and then the object claims that it implements these interfaces. Of course, you can use this object as any of these interfaces. Of course, this is actually a proxy object. It will not do substantive work for you. When generating its instance, you must provide a handler to take over the actual work.

Interface:

public interface StudentDao { 
  public void test1(); 
  public void test2(); 
  public void test3(); 
  public void test4(); 
}
​
public interface StudentDao2 { 
  public void t(); 
} 

Target class:

public class StudentDaoImpl implements StudentDao ,StudentDao2{ 
    //Implementation of interface method 
}
Handler: 

public class MyInvocationHandler implements InvocationHandler { 
  private Object target; 
  private List<String> matchNames=null; //To control the functional changes to those methods of the target class
  public MyInvocationHandler() { 
  } 
  public MyInvocationHandler(Object target) { 
    this.target = target; 
    matchNames=new ArrayList<String>(); 
    matchNames.add("test1"); 
    matchNames.add("test2"); 
    matchNames.add("test3");     
  } 
  @Override 
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 
    String methodName=method.getName(); 
    if(matchNames.contains(methodName)){ 
      System.out.println("========begin transaction========="); 
    } 
    Object ret = method.invoke(target, args); 
    if(matchNames.contains(methodName)){ 
      System.out.println("========end transaction=========="); 
    } 
    return ret; 
  } 
}

Test class:

 public static void main(String[] args) { 
    StudentDaoImpl dao=new StudentDaoImpl(); 
    MyInvocationHandler handler=new MyInvocationHandler(dao); 
    StudentDao proxy=(StudentDao)Proxy.newProxyInstance(
                                        dao.getClass().getClassLoader(),
                                        new Class<?>[]{StudentDao.class},
                                        handler); 
    proxy.test1(); 
    proxy.test2(); 
    proxy.test3(); 
    proxy.test4(); 
}

Operation result:

========begin transaction=========
==========Target method: test1()============
========end transaction==========
========begin transaction=========
==========Target method: test2()============
========end transaction==========
========begin transaction=========
==========Target method: test3()============
========end transaction==========
==========Target method: test4()============

Original link: https://blog.51cto.com/yangfei520/245254 Source network, only for learning, if there is infringement, contact delete.

I have collected high-quality technical articles and experience summary in my official account [Java circles].

In order to facilitate your study, I have compiled a set of learning materials, including Java virtual machine, spring framework, java thread, data structure, design mode, etc., which are free for students who love java! More learning and communication groups, more communication problems to make faster progress~

Posted by nyk on Sat, 04 Apr 2020 15:18:16 -0700