CGLIB - dynamic bytecode generation

关键字:   cglib    

CGLIB (Code Generation Library) 是一个动态字节码生成库, 基于ASM, 使用时需要asm.jar。(Groovy也是使用ASM来生成java的字节码)

使用java的java.lang.reflect.Proxy只能针对接口生成代理; 而使用CGLIB则可以针对类生成代理. CGLIB原理是在运行时动态生成一个被代理类的子类。

下面的例子使用CGLIB, 对服务层对象的方法进行拦截,实现一个类似的AOP.

java 代码
  1. public class ProductService implements ServiceInterface  {   
  2.        
  3.     public void doDelete(Integer id) {   
  4.         productDAO.delete(id);   
  5.     }   
  6.   
  7.     public void doSave(ProductDTO dto) {   
  8.         productDAO.saveOrUpdate(dto);   
  9.     }   
  10.   
  11.     public void doUpdate(ProductDTO dto) {   
  12.         productDAO.saveOrUpdate(dto);   
  13.     }   
  14.   
  15.     public ProductDTO getObject(Integer id) {   
  16.         return productDAO.getProduct(id);   
  17.     }   
  18. }  

ProductService是一个简单的服务层类, 在ServiceInterface中定义了DAO对象和一些方法。

如果服务层对象有事物方面的需要, 就使用Interceptor来增强. 需要实现 net.sf.cglib.proxy.MethodInterceptor 接口.

java 代码
  1. public class ServiceMethodInterceptor implements MethodInterceptor {   
  2.        
  3.     private static final Logger logger = Logger.getLogger(ServiceMethodInterceptor.class);   
  4.   
  5.     public Object intercept(Object o, Method method, Object[] args, MethodProxy proxy) throws Throwable {   
  6.   
  7.         if (method == null || StringUtils.isBlank(method.getName())) {   
  8.             logger.error("Method is null!");   
  9.             return null;   
  10.         }   
  11.         String methodName = method.getName();   
  12.            
  13.         //所有以do开头的方法,使用updateTransaction   
  14.         if (methodName.startsWith("do")) {   
  15.             return updateTransaction(o, method, args, proxy);   
  16.         }   
  17.            
  18.         //所有以get开头的方法,使用queryTransaction   
  19.         if (methodName.startsWith("get")) {   
  20.             return queryTransaction(o, method, args, proxy);   
  21.         }   
  22.            
  23.         return proxy.invokeSuper(o,args);   
  24.     }   
  25.        
  26.     private Object queryTransaction(Object o, Method method, Object[] args, MethodProxy proxy) throws Throwable {   
  27.   
  28.         Object result = null;   
  29.         try{   
  30.             result = proxy.invokeSuper(o,args);   
  31.         } catch (Exception e) {   
  32.             logger.fatal(e);   
  33.         }  finally {   
  34.             TransactionBO.endSession();   
  35.         }   
  36.            
  37.         return result;   
  38.     }   
  39.        
  40.     private Object updateTransaction(Object o, Method method, Object[] args, MethodProxy proxy) throws Throwable {   
  41.            
  42.         Object result = null;   
  43.         try{   
  44.             TransactionBO.beginTransaction();   
  45.             result = proxy.invokeSuper(o,args);   
  46.             TransactionBO.commit();   
  47.         } catch (Exception e) {   
  48.             TransactionBO.rollback();   
  49.         }  finally {   
  50.             TransactionBO.endSession();   
  51.         }   
  52.            
  53.         return result;   
  54.     }   
  55.   
  56. }  
java 代码
  1. public class ServiceFactory {   
  2.        
  3.     public static extends ServiceInterface> T getServiceInstance(Class clazz) {   
  4.         Enhancer enhancer = new Enhancer();   
  5.         enhancer.setSuperclass(clazz);   
  6.         enhancer.setCallback(new ServiceMethodInterceptor());   
  7.         return (T) enhancer.create();   
  8.     }   
  9. }  

使用ProductService时,需要由ServiceFactory创建

java 代码
  1. public static void main(String[] args) {   
  2.     ProductService productService = ServiceFactory.getServiceInstance(ProductService.class);   
  3.     productService.doSave(...);   
  4.     productService.getObject(...);   
  5. }  

实际上这里使用的是CGLIB生成的ProductService的子类.  Enhancer生成的子类都实现了net.sf.cglib.proxy.Factory接口, 可以通过Factory.getCallbacks()取回MethodInterceptor

参考

http://cglib.sourceforge.net/

http://asm.objectweb.org/

http://www.javaeye.com/topic/98178

http://www.blogjava.net/calvin/archive/2005/11/28/21741.html

http://www-128.ibm.com/developerworks/cn/java/j-dyn0414/index.html

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章