Detailed dynamic agent -- JDK dynamic agent and cglib dynamic agent

1, Role of dynamic agents

  1. Add additional business functions without changing the original business code

  2. Reduce code duplication

  3. Focus on business logic code

  4. Decouple business functions from non business functions such as logs and transactions

2, Implementation mode and principle

1. Principle

      You can create proxy objects during program execution,

      The method is executed through the proxy object to add additional functions (function enhancement) to the methods of the target class

2. Implementation mode

(1) jdk dynamic agent

          Create a proxy object using the proxy method invocationhandler in the jdk

          Write the specific added function code in the invoke method

          Requirement: the target class is an interface implementation class and must implement an interface

  (2) cglib dynamic proxy

          The principle of creating proxy objects using third-party tool libraries is inheritance

          Create a subclass by inheriting the target class, which is the proxy object (the function extension of the subclass to the parent class)

          Requirement: the target class can be inherited, that is, the target class and its methods cannot be final

3, jdk dynamic agent

1. Implementation steps     

(1) Create InvocationHandler implementation class and call method and custom function class methods

          Implement the added functions in the invoke method

  (2) Create three objects:

  •           The target class object and InvocationHandler implementation class object,
  •           Pass the target class object as a parameter into the InvocationHandler implementation class object
  •           Using proxy class to create proxy object -- newProxyInstance    
  •           The target class uses the reflection mechanism to give the class loader and the implemented interface to the proxy object,
  •           The proxy object is used to call the method of the target class, and the invoke method is called automatically to increase the function of the method

2. Code implementation

  Requirements: add two business functions to each method of the target class: output time before the method and commit transaction after the method

  •     Target class -- interface implementation class
public class SomeSercieImpl implements SomeServie{
   //Method 1
   public void doSome(){

    System.out.println("this is doSome");
   //Method 2
   public void doOther(){

    System.out.println("this is doOther");
  • InvocationHandler implementation class -- invoke
public class MyInvocationHandler implements InvocationHandler{

   //1. Declare the target object as a property and construct the injection
     private Object target;
     public MyInvocationHandler (Object target){; 

  //2. Write the function to be added in the invoke method
   public Object invoke(Object proxy,Method method,Object[] args ){  
     Object res=null;
     //2.1 output time before method 
     //2.2 calling method
     res.method.invoke(target,args);//Target refers to the target class, and args refers to the parameters of the methods in the class
     //2.3 post method commit transaction
      return res; 
  • Encapsulate new business classes
public class ServiceUtil{

   //Output time
   public static void doLog(){
       System.out.println("date:"+new Date()); 

   //Commit transaction
   public static void doTrans(){
       System.out.println("Commit Transaction"); 

Test -- proxy create proxy object and call method -- newProxyInstance         

  • Create target class object
  • Create InvocationHandler implementation class object,
  •   Pass the target class object as a parameter into the InvocationHandler implementation class object
  •   Create proxy objects using proxy classes,
public static void main(String[] args){

//1. Create target object
   SomeServie target=new SomeServieImpl();
//2. Create InvocationHandler implementation class object
 InvocationHandler handler=new MyInvocationHandler(target);
//3. Create a Proxy object of SomeServie class through the Proxy class
  SomeServie proxy=()Proxy.newProxyInstance(target.getClass().getClassLoader(),
                                            //Target class loader, target class interface method, dynamic proxy, implementation class object
//4. Proxy object invokes target class method - add function during execution

Tags: Java Interview

Posted on Sat, 18 Sep 2021 08:50:06 -0400 by briansol