利用Spring AOP与JAVA注解为系统增加日志功能 利用Spring AOP与JAVA注解为系统增加日志功能

Spring AOP一直是Spring的一个比较有特色的功能,利用它可以在现有的代码的任何地方,嵌入我们所想的逻辑功能,并且不需要改变我们现有的代码结构。

 

鉴于此,现在的系统已经完成了所有的功能的开发,我们需要把系统的操作日志记录起来,以方便查看某人某时执行了哪一些操作。Spring AOP可以方便查看到某人某时执行了哪一些类的哪一些方法,以及对应的参数。但是大部分终端用户看这些方法的名称时,并不知道这些方法名代码了哪一些操作,于是方法名对应的方法描述需要记录起来,并且呈现给用户。我们知道,AOP拦截了某些类某些方法后,我们可以取得这个方法的详细定义,通过详细的定义,我们可以取得这个方法对应的注解,在注解里我们就可以比较方便把方法的名称及描述写进去。于是,就有以下的注解定义。代码如下所示:

 

Java代码     收藏代码
  1. package com.htsoft.core.log;  
  2.   
  3. import java.lang.annotation.Documented;  
  4. import java.lang.annotation.ElementType;  
  5. import java.lang.annotation.Inherited;  
  6. import java.lang.annotation.Retention;  
  7. import java.lang.annotation.RetentionPolicy;  
  8. import java.lang.annotation.Target;  
  9.   
  10. /** 
  11.  * @company  广州宏天软件有限公司 
  12.  * @description 类的方法描述注解 
  13.  * @author csx 
  14.  * @create 2010-02-03 
  15.  */  
  16. @Target(ElementType.METHOD)     
  17. @Retention(RetentionPolicy.RUNTIME)     
  18. @Documented    
  19. @Inherited    
  20. public @interface Action {  
  21.     /** 
  22.      * 方法描述 
  23.      * @return 
  24.      */  
  25.     public String description() default "no description";   
  26. }  

 

 

在我们需要拦截的方法中加上该注解:

 

 

Java代码     收藏代码
  1. /** 
  2.  *  
  3.  * @author csx 
  4.  *  
  5.  */  
  6. public class AppUserAction extends BaseAction {   
  7.     /** 
  8.      * 添加及保存操作 
  9.      */  
  10.     @Action(description="添加或保存用户信息")  
  11.     public String save() {  
  12.              ....  
  13.         }  
  14.        /** 
  15.      * 修改密码 
  16.      *  
  17.      * @return 
  18.      */  
  19.     @Action(description="修改密码")  
  20.     public String resetPassword() {  
  21.               ....  
  22.         }  
  23. }  

 

现在设计我们的系统日志表,如下所示:

  

设计嵌入的逻辑代码,以下类为所有Struts Action的方法都需要提前执行的方法。(对于get与set的方法除外,对于没有加上Action注解的也除外)

 

Java代码     收藏代码
  1. package com.htsoft.core.log;  
  2.   
  3. import java.lang.reflect.Method;  
  4. import java.util.Date;  
  5.   
  6. import javax.annotation.Resource;  
  7.   
  8. import org.apache.commons.lang.StringUtils;  
  9. import org.apache.commons.logging.Log;  
  10. import org.apache.commons.logging.LogFactory;  
  11. import org.aspectj.lang.ProceedingJoinPoint;  
  12.   
  13. import com.htsoft.core.util.ContextUtil;  
  14. import com.htsoft.oa.model.system.AppUser;  
  15. import com.htsoft.oa.model.system.SystemLog;  
  16. import com.htsoft.oa.service.system.SystemLogService;  
  17.   
  18. public class LogAspect {  
  19.       
  20.     @Resource  
  21.     private SystemLogService systemLogService;  
  22.       
  23.     private Log logger = LogFactory.getLog(LogAspect.class);  
  24.   
  25.     public Object doSystemLog(ProceedingJoinPoint point) throws Throwable {  
  26.   
  27.         String methodName = point.getSignature().getName();  
  28.   
  29.         // 目标方法不为空  
  30.         if (StringUtils.isNotEmpty(methodName)) {  
  31.             // set与get方法除外  
  32.             if (!(methodName.startsWith("set") || methodName.startsWith("get"))) {  
  33.   
  34.                 Class targetClass = point.getTarget().getClass();  
  35.                 Method method = targetClass.getMethod(methodName);  
  36.   
  37.                 if (method != null) {  
  38.   
  39.                     boolean hasAnnotation = method.isAnnotationPresent(Action.class);  
  40.   
  41.                     if (hasAnnotation) {  
  42.                         Action annotation = method.getAnnotation(Action.class);  
  43.                           
  44.                         String methodDescp = annotation.description();  
  45.                         if (logger.isDebugEnabled()) {  
  46.                             logger.debug("Action method:" + method.getName() + " Description:" + methodDescp);  
  47.                         }  
  48.                         //取到当前的操作用户  
  49.                         AppUser appUser=ContextUtil.getCurrentUser();  
  50.                         if(appUser!=null){  
  51.                             try{  
  52.                                 SystemLog sysLog=new SystemLog();  
  53.                                   
  54.                                 sysLog.setCreatetime(new Date());  
  55.                                 sysLog.setUserId(appUser.getUserId());  
  56.                                 sysLog.setUsername(appUser.getFullname());  
  57.                                 sysLog.setExeOperation(methodDescp);  
  58.                                   
  59.                                 systemLogService.save(sysLog);  
  60.                             }catch(Exception ex){  
  61.                                 logger.error(ex.getMessage());  
  62.                             }  
  63.                         }  
  64.                           
  65.                     }  
  66.                 }  
  67.   
  68.             }  
  69.         }  
  70.         return point.proceed();  
  71.     }  
  72.   
  73. }  
 

通过AOP配置该注入点:

 

Java代码     收藏代码
  1. <aop:aspectj-autoproxy/>   
  2. <bean id="logAspect" class="com.htsoft.core.log.LogAspect"/>    
  3.  <aop:config>  
  4.         <aop:aspect ref="logAspect">  
  5.             <aop:pointcut id="logPointCut" expression="execution(* com.htsoft.oa.action..*(..))"/>  
  6.             <aop:around pointcut-ref="logPointCut" method="doSystemLog"/>  
  7.         </aop:aspect>  
  8. </aop:config>  
 

  注意,由于AOP的默认配置是使用代理的方式进行嵌入代码运行,而StrutsAction中若继承了ActionSupport会报错误,错误是由于其使用了默认的实现接口而引起的。所以Action必须为POJO类型。

 

如我们操作了后台的修改密码,保存用户信息的操作后,系统日志就会记录如下的情况。

 

猜你喜欢

转载自zqs520.iteye.com/blog/2288429