@AfterReturning配置

来源:互联网 发布:linux配置环境变量 编辑:程序博客网 时间:2024/05/19 10:40

AfterReturning和before,after,around不同是可以获取织入函数的返回值,配置复杂一点。

如果切入点如下:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package com.leanyu.lcsystem.pointcut;  
  2.   
  3. import org.aspectj.lang.annotation.Aspect;  
  4. import org.aspectj.lang.annotation.Pointcut;  
  5.   
  6. /** 
  7.  * @author pandlvs 
  8.  *描述:切入点定义 
  9.  */  
  10. @Aspect  
  11. public class PointcutsDefinition   
  12. {  
  13.   
  14.      public static final String ASPECT_SERVICE_EXCUTION = "execution(* com.leanyu.lcsystem.service.*.*(..))";  
  15.        
  16.      /** 
  17.       * 服务层的切入点 
  18.       */  
  19.      @Pointcut(ASPECT_SERVICE_EXCUTION)  
  20.      public void inServiceLayer(){}  
  21. }  

则around和AfterReturning的配置区别如下,注意AfterReturning配置必须有argNames参数,且参数值和returning值一样,这样在织入代码里面便可通过returning的值获取被织入函数的返回值。

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package com.leanyu.lcsystem.permission.impl;  
  2.   
  3. import org.apache.log4j.Logger;  
  4. import org.aspectj.lang.JoinPoint;  
  5. import org.aspectj.lang.ProceedingJoinPoint;  
  6. import org.aspectj.lang.Signature;  
  7. import org.aspectj.lang.annotation.AfterReturning;  
  8. import org.aspectj.lang.annotation.Around;  
  9. import org.aspectj.lang.annotation.Aspect;  
  10. import org.springframework.beans.factory.annotation.Autowired;  
  11.   
  12. import com.leanyu.lcsystem.beans.UserBean;  
  13. import com.leanyu.lcsystem.permission.ICheckPermission;  
  14. import com.leanyu.lcsystem.service.log.SyslogService;  
  15. import com.leanyu.rpc.request.AbstractRequest;  
  16. import com.leanyu.rpc.response.ObjectResponse;  
  17. import com.leanyu.rpc.response.RpcResponse;  
  18. /** 
  19.  * @author pandlvs 
  20.  *  
  21.  * 使用@Aspect 注解的类, Spring 将会把它当作一个特殊的Bean(一个切面),也就是  
  22.  * 不对这个类本身进行动态代理  
  23.  */    
  24. @Aspect  
  25. public class PermissionInterceptor     
  26. {  
  27.     private Logger logger = Logger.getLogger(PermissionInterceptor.class);  
  28.       
  29.     @Autowired  
  30.     private ICheckPermission checkPermission;  
  31.       
  32.     @Autowired  
  33.     private SyslogService syslogService;  
  34.       
  35.     /** 
  36.      * spring中Around通知   
  37.      * @param joinPoint 
  38.      * @return 
  39.      * @throws Throwable  
  40.      */  
  41.     @Around("com.leanyu.lcsystem.pointcut.PointcutsDefinition.inServiceLayer()")     
  42.     public Object checkPermissionAround( ProceedingJoinPoint joinPoint )    
  43.     {  
  44.         try  
  45.         {   
  46.              Object[] args           = joinPoint.getArgs();  
  47.              AbstractRequest request =  (AbstractRequest)args[0];  
  48.              int userId          = request.getUserId();  
  49.              String password     = request.getPassword();  
  50.              Signature signature = joinPoint.getSignature();  
  51.              String service      = signature.getDeclaringTypeName();  
  52.              String method       = signature.getName();  
  53.                            
  54.              if ( checkPermission.hasServicePermission(userId, service , method , password) )  
  55.              {  
  56.                  Object result = joinPoint.proceed( args );    
  57.                  return result;  
  58.              }  
  59.                
  60.          }catch(Throwable e)  
  61.          {  
  62.              logger.error(e.toString(), e);  
  63.          }  
  64.          return null;  
  65.     }  
  66.     /** 
  67.      * AfterReturning 三个参数必须有 
  68.      * @param joinPoint 
  69.      * @param retVal 
  70.      */  
  71.     @SuppressWarnings("unchecked")  
  72.     @AfterReturning(value = "com.leanyu.lcsystem.pointcut.PointcutsDefinition.inServiceLayer()",argNames = "retVal",returning = "retVal")  
  73.     public void logInfo(JoinPoint joinPoint,Object retVal)  
  74.     {  
  75.         Object[] object = joinPoint.getArgs();  
  76.           
  77.         if(object.length>1return;  
  78.           
  79.         int userId;  
  80.         if("logout".equals(joinPoint.getSignature().getName()) ||   
  81.             "login".equals(joinPoint.getSignature().getName())){  
  82.               
  83.             if(((ObjectResponse<UserBean>)retVal).getErrorCode()!=0return;  
  84.               
  85.             userId = ((ObjectResponse<UserBean>)retVal).getData().getUserId();  
  86.               
  87.         }else{  
  88.             if("query".equals(joinPoint.getSignature().getName().substring(05))) return;  
  89.             if(((RpcResponse)retVal).getErrorCode()!=0return;  
  90.               
  91.             userId = ((AbstractRequest)object[0]).getUserId();  
  92.         }  
  93.           
  94.         String action = joinPoint.getSignature().getName();  
  95.         String service = joinPoint.getSignature().getDeclaringTypeName();  
  96.           
  97.         syslogService.addSyslog(userId, action, service);  
  98.     }  
  99.        
  100. }  
0 0