CGLIB instance of Spring AOP

CGLIB instance of Spring AOP

 

Using a CGLIB proxy

 

      1. The Advice class should implement the MethodInterceptor interface

      2. To define an agent factory by yourself, to configure target and Advice

 

 

business class

package com.yeepay.porxy.cglib.test;

import java.util.HashMap;
import java.util.Map;

/**
 * target class, (delegate class)
 * @author shangxiaofei
 *
 */
public class PaymentServer {

    public Map<String, String> payMoney(String name,Integer money){
        System.out.println("paymentServer.payMoney()付钱=========>名字【"+name+"】,money=【"+money+"】");
        Map<String, String> map=new HashMap<String, String>();
        map.put("result", "Payment completed");
        return map;
    }
}

 

Advice class

package com.yeepay.porxy.cglib.test;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
/**
 * Enhanced class
 * @author shangxiaofei
 *
 */
public class PoxyForService implements MethodInterceptor{

    @Override
    public Object intercept(Object arg0, Method arg1, Object[] arg2,MethodProxy arg3) throws Throwable {
          // code for enhancement
        System.out.println("PoxyForService.intercept(execute before method execution========>send a message to statistics)");
        if(arg2!=null&&arg2.length>0){
            for(int i=0;i<arg2.length;i++){
                System.out.println("Parameter content sent by PoxyForService.intercept()====>"+arg2[i]);
            }
        }
        
        
        // execute method content
        Object object=arg3.invokeSuper(arg0, arg2);
        
        
        System.out.println("PoxyForService.intercept(execution after method execution =======> calculation time)");
        return object;
    }
    
    

}

 

Proxy factory class

package com.yeepay.porxy.cglib.test;

import net.sf.cglib.proxy.Enhancer;

/**
 * The proxy implements the factory class
 * @author shangxiaofei
 *
 */
public class PoxyFactory {
     public static PaymentServer getPaymentServer(){
         // Cglib 核心类,生成代理对象
        Enhancer enhancer= new Enhancer();
         // 为核心类对象中放入需要代理的委托类的类对象
        enhancer.setSuperclass(PaymentServer.class);
         // 为核心类对象中放入增强功能的类对象
        enhancer.setCallback(new PoxyForService());
         // 从核心类对象中获取委托类的代理对象
        Object object=enhancer.create();
        
        return (PaymentServer) object;
     }
}

 

测试类

package com.yeepay.porxy.cglib.test;

import java.util.Map;
/**
 * 测试类
 * @author shangxiaofei
 *
 */
public class TestController {

    private PaymentServer paymentServer=PoxyFactory.getPaymentServer();
    
    public void payment(){
        System.out.println("TestController.payment()开始");
        Map<String, String> map=paymentServer.payMoney("怪物雷克", 100);
        System.out.println("TestController.payment()结束===>"+map.get("result"));
    }
    
    
    /**
     * TestController.payment()开始
     * PoxyForService.intercept(方法执行前执行========>向统计发送消息)
     * PoxyForService.intercept()发送的参数内容====>怪物雷克
     * PoxyForService.intercept()发送的参数内容====>100
     * paymentServer.payMoney()付钱=========>名字【怪物雷克】,money=【100】
     * PoxyForService.intercept(方法执行后执行=======>计算时间)
     * TestController.payment()结束===>已经完成付钱
     * @param args
     */
    public static void main(String[] args) {
        TestController testController=new TestController();
        testController.payment();
    }
}

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326567639&siteId=291194637