java代理机制简单实现

来源:互联网 发布:淘宝的促销大师怎么用 编辑:程序博客网 时间:2024/06/05 07:18
 java代理分静态代理和动态代理,动态代理有jdk代理和cglib代理两种,在运行时生成新的子类class文件。本文主要练习下动态代理,代码用于备忘。对于代理的原理和机制,网上有很多写的很好的,就不班门弄斧了。 

  • jdk代理下载 
Java代码   收藏代码
  1. import java.lang.reflect.InvocationHandler;  
  2. import java.lang.reflect.Method;  
  3. import java.lang.reflect.Proxy;  
  4.   
  5. public class ProxyFactory implements InvocationHandler {    
  6.         
  7.     private Object tarjectObject;    
  8.     
  9.     public Object creatProxyInstance(Object obj) {    
  10.         this.tarjectObject = obj;  
  11.         return Proxy.newProxyInstance(this.tarjectObject.getClass()    
  12.                 .getClassLoader(), this.tarjectObject.getClass()    
  13.                 .getInterfaces(), this);    
  14.     }    
  15.     
  16.     @Override    
  17.     public Object invoke(Object proxy, Method method, Object[] args)    
  18.             throws Throwable {   
  19.         Object result = null;    
  20.         if (AssessUtils.isAssess()) {    
  21.             result = method.invoke(this.tarjectObject, args);    
  22.         }else{  
  23.             throw new NoAssessException("This server cannot run this service.");  
  24.         }  
  25.         return result;    
  26.     }  
  27. }  


  • cglib代理下载 
Java代码  收藏代码
  1. import java.lang.reflect.Method;  
  2. import org.springframework.cglib.proxy.Enhancer;  
  3. import org.springframework.cglib.proxy.MethodInterceptor;  
  4. import org.springframework.cglib.proxy.MethodProxy;  
  5.   
  6. public class ProxyCglibFactory implements MethodInterceptor {    
  7.         
  8.     private Object tarjectObject;    
  9.     
  10.     public Object creatProxyInstance(Object obj) {    
  11.         this.tarjectObject = obj;  
  12.         Enhancer enhancer=new Enhancer();  
  13.         enhancer.setSuperclass(this.tarjectObject.getClass());  
  14.         enhancer.setCallback(this);  
  15.         return enhancer.create();  
  16.     }  
  17.   
  18.     @Override  
  19.     public Object intercept(Object obj, Method method, Object[] args,  
  20.             MethodProxy arg3) throws Throwable {  
  21.         Object result = null;    
  22.         if (AssessUtils.isAssess()) {    
  23.             result = method.invoke(this.tarjectObject, args);    
  24.         }else{  
  25.             throw new NoAssessException("This server cannot run this service.");  
  26.         }  
  27.         return result;    
  28.     }  
  29. }  


  • Aspect注解下载 
Java代码 下载  收藏代码
  1. import org.aspectj.lang.JoinPoint;  
  2. import org.aspectj.lang.ProceedingJoinPoint;  
  3. import org.aspectj.lang.annotation.Around;  
  4. import org.aspectj.lang.annotation.Aspect;  
  5. import org.aspectj.lang.annotation.Before;  
  6. import org.aspectj.lang.annotation.Pointcut;  
  7.   
  8. @Aspect  
  9. public class AssessInterceptor {  
  10.     @Pointcut(value="execution (* com..*.*(..))")  
  11.     private void anyMethod(){};  
  12.       
  13.     @Before("anyMethod()")  
  14.     public void doBefore(JoinPoint joinPoint) throws NoAssessException{  
  15.         if (!AssessUtils.isAssess()) {    
  16.             throw new NoAssessException("This server cannot run this service.");  
  17.         }  
  18.     }  
  19.       
  20.     /** 
  21.      * Around异常的时候调用 
  22.      * @param pjp 
  23.      * @throws Throwable 
  24.      */  
  25.     @Around("anyMethod()")  
  26.     public void invoke(ProceedingJoinPoint pjp) throws Throwable{  
  27.         pjp.proceed();    
  28.     }  
  29.   
  30. }  
0 0