Spring AOP 之CGLIB实例

Spring AOP 之CGLIB实例

 

使用的是CGLIB代理

 

      1. Advice类要实现MethodInterceptor接口

      2. 要自己定义一个代理工厂,用于配置target和Advice

 

 

业务类

package com.yeepay.porxy.cglib.test;

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

/**
 * 目标类,(委托类)
 * @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", "已经完成付钱");
        return map;
    }
}

 

Advice类

package com.yeepay.porxy.cglib.test;

import java.lang.reflect.Method;

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

    @Override
    public Object intercept(Object arg0, Method arg1, Object[] arg2,MethodProxy arg3) throws Throwable {
          // 增强功能的代码
        System.out.println("PoxyForService.intercept(方法执行前执行========>向统计发送消息)");
        if(arg2!=null&&arg2.length>0){
            for(int i=0;i<arg2.length;i++){
                System.out.println("PoxyForService.intercept()发送的参数内容====>"+arg2[i]);
            }
        }
        
        
        //执行方法内容
        Object object=arg3.invokeSuper(arg0, arg2);
        
        
        System.out.println("PoxyForService.intercept(方法执行后执行=======>计算时间)");
        return object;
    }
    
    

}

 

代理的工厂类

package com.yeepay.porxy.cglib.test;

import net.sf.cglib.proxy.Enhancer;

/**
 * 代理实现工厂类
 * @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();
    }
}

 

 

猜你喜欢

转载自youyu4.iteye.com/blog/2348478