Acting Acting -cglib

Jdk dynamic proxy for the interface can only go to complete the operation, but it can do cglib agent class does not implement the interface, you can also do it as a proxy class that implements the interface.

IDB

package com.bjpowernode.proxy;

/**
 * Proxy class and target class must use the same interface. 
 */
public interface IDB {
    
    int insert();
    int delete();
    int update();
}

OracleDB 

package com.bjpowernode.proxy;

/**
 * This is an Oracle database-related operations class
 * 
 * Target class (delegate class).
 */
public class OracleDB implements IDB{
    public int insert(){
        
        // The following is an insert 
        System.out.println ( "INSERT the Oracle Data ...." );
         the try {
            Thread.sleep(526);
        } catch (InterruptedException e) {
            e.printStackTrace ();
        }
        return 0;
    }
    
    
    public int delete(){
        
        // The following is a delete operation 
        System.out.println ( "the Oracle the Delete the Data ...." );
         the try {
            Thread.sleep(569);
        } catch (InterruptedException e) {
            e.printStackTrace ();
        }
        
        return 0;
    }
    
    
    public int update(){
        
        // The following is an update operation 
        System.out.println ( ".... the Oracle Data Update" );
         the try {
            Thread.sleep(456);
        } catch (InterruptedException e) {
            e.printStackTrace ();
        }
        
        return 0;
    }
    
}

CglibProxyFactory

public  class CglibProxyFactory the implements MethodInterceptor {
     // get the target object 
    Private Object target;

    // pass the target object using the constructor 
    public CglibProxyFactory (Object target) {
         Super ; ()
         the this .target = target;
    }


    // Proxy agent is a reference object; method a target is the target class; args argument target method is a target class; four parameters: the agent needs a method to enhance 
    public Object Intercept (Object Proxy, Method, Method, Object [] args, methodProxy methodProxy) throws the Throwable {
        System.out.println ( "This is a pre-enhancement ......" );
        Object invoke = methodProxy.invoke(target, args);
        System.out.println ( "This is the method of enhancing ......" );
         return the Invoke;
    }

}

Test 

import java.lang.reflect.Proxy;

public class Test {

    public static void main(String[] args) {
        
        // create the target class object 
        IDB db = new new OracleDB ();
        
        // 1. Create Enhancer 
        Enhancer Enhancer = new new Enhancer ();
         // 2. transfer target object class 
        enhancer.setSuperclass (db.getClass ());
         // 3. disposed callback operations 
        IDB dbProxy = (IDB) enhancer.setCallback ( new new CglibProxyFactory (DB));
        
        
        // to execute the method of the target object by means of a proxy object. 
        dbProxy.insert ();
        dbProxy.delete(); 
        dbProxy.update();
        
    }

}

 

Guess you like

Origin www.cnblogs.com/yangyanbo/p/12370156.html