Spring Aop实现之CGLIB实现

spring aop的底层实现有两种方式,一种是基于jdk的动态代理,只针对接口,还有一种是基于CGlib,这里结合springapp(spring官方示例项目)及《spring3.x企业应用开发实战》进行对基于CGlib的方式进行详细的阐述,代码示例可下载:

在项目原有的基础上下载CGLIB支持包,下载地址:http://cglib.sourceforge.net/

创建代理类:

package springapp.util;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

 
/**     
 * CGlib 代理类继承自MethodInterceptor     
 * @author zhangxuegang     
 * @version 1.0     
 * @created 2012-10-11 下午01:17:20    
 */

public class CglibProxy implements MethodInterceptor {
    
     private Enhancer enhancer = new Enhancer();
     public Object getProxy(Class clazz) {
      enhancer.setSuperclass(clazz);
      enhancer.setCallback(this);
      return enhancer.create(); 
     }
     
     public Object intercept(Object obj, Method method, Object[] args,
       MethodProxy proxy) throws Throwable {
      PerformanceMonitor.begin(obj.getClass().getName()+"."+method.getName());
      Object result=proxy.invokeSuper(obj, args);
      PerformanceMonitor.end();
      return result;
     }
      
    }

性能监控代码:

package springapp.util;

  
/**     
 *      
 * 性能监视代码   
 * @author zhangxuegang       
 * @version 1.0     
 * @created 2012-10-11 下午01:19:27    
 */

public class PerformanceMonitor {

      
     //通过一个ThreadLocal保存线程相关的性能监视信息
     private static ThreadLocal<MethodPerformace> performaceRecord =
    new ThreadLocal<MethodPerformace>();
     public static void begin(String method) {
      System.out.println("begin monitor...");
      MethodPerformace mp = new MethodPerformace(method);
      performaceRecord.set(mp);
     }
     public static void end() {
      System.out.println("end monitor...");
      MethodPerformace mp = performaceRecord.get();
      mp.printPerformace(); //打印出业务方法性能监视的信息
     }

}

待测试的业务代码:

package springapp.util;


import springapp.service.PersonsManager;

/**
 * 
 * 模拟的业务代码
 * @author zhangxuegang
 * @version 1.0
 * @created 2012-10-11 下午01:54:29
 */

public class ForumServiceImpl {

    /**
     * 描述
     * 
     * @param i
     */
    public void searchForum(int i) {
        for(int i1=1 ; i1<10000;i1++){
            System.out.println("i");
            i1=i1+1;
        }
    }

    /**
     * 描述
     * 
     * @param i
     */
    public void searchTopic(int i) {
        System.out.println("99999999999999999");
    }


}

测试代码:

package springapp.util;

  
/**     
 *      
 *     
 * @author zhangxuegang       
 * @version 1.0     
 * @created 2012-10-11 下午01:52:53    
 */

public class TestForumService {
     public static void main(String[] args) {
           CglibProxy proxy = new CglibProxy();
           ForumServiceImpl forumService = //① 通过动态生成子类的方式创建代理对象
        (ForumServiceImpl )proxy.getProxy(ForumServiceImpl.class);
           forumService.searchForum(10);
           forumService.searchTopic(1023);
         }
}

输出结果:

begin monitor...
i
i
i
i
i
end monitor...
springapp.util.ForumServiceImpl$$EnhancerByCGLIB$$4ec7c3e4.searchForum花费21毫秒。
begin monitor...
99999999999999999
end monitor...
springapp.util.ForumServiceImpl$$EnhancerByCGLIB$$4ec7c3e4.searchTopic花费0毫秒。
posted @ 2012-10-10 13:58  Mr-sniper  阅读(430)  评论(0编辑  收藏  举报