天天看点

SpringAop在项目中的一些巧妙使用(二)--记录日志

转载自:http://743389831.iteye.com/blog/1755631

在(一)中我们对AOP加动态代理有了初步认识,那如何使用这个记录用户进行了哪些操作呢?我们已经知道,AOP加动态代理我们可以知道用户都做了什么调用了哪些方法。我们也知道这些方法是干嘛用的,难道我们要写一个代码if调用了这个类的某某方法,那么这个用户做了什么什么....这个明显太复杂。如果我们对一些方法加注释,并且能获得这个注释,我们是不是就把问题解决了呢?那怎样的注释是我们代码能获得呢?解决了这个问题就差不多把问题解决了。

     首先你进行注释,使用注解注释,第一步生成注解。

Java代码  

SpringAop在项目中的一些巧妙使用(二)--记录日志
  1. package net.zoneland.test.common.dal;  
  2. import net.zoneland.test.common.annotation.Log;  
  3. public interface TestMapper {  
  4.     @Log(name = "某某的维护或者配置", comments = "更新。。。。")  
  5.     public int updateByPrimaryKey();  
  6.     @Log(name = "某某的维护或者配置", comments = "增加。。。。")  
  7.     public void insert();  
  8.     @Log(name = "某某的维护或者配置", comments = "删除。。。。")  
  9.     public int deleteByPrimaryKey();  
  10. }  

 对方法进行注释:

Java代码  

SpringAop在项目中的一些巧妙使用(二)--记录日志
  1. package net.zoneland.test.common.dal;  
  2. import net.zoneland.ums.common.util.annotation.Log;  
  3. public interface TestMapper {  
  4.     @Log(name = "某某的维护或者配置", comments = "更新。。。。")  
  5.     public int updateByPrimaryKey();  
  6.     @Log(name = "某某的维护或者配置", comments = "增加。。。。")  
  7.     public void insert();  
  8.     @Log(name = "某某的维护或者配置", comments = "删除。。。。")  
  9.     public int deleteByPrimaryKey();  
  10. }  

 然后进行监控操作跟上面记录方法执行时间差不多。

Java代码  

SpringAop在项目中的一些巧妙使用(二)--记录日志
  1. package net.zoneland.test.common.annotation;  
  2. import java.lang.reflect.Method;  
  3. import org.apache.log4j.Logger;  
  4. import org.springframework.aop.MethodBeforeAdvice;  
  5. public class LogTraceAdvice implements MethodBeforeAdvice {  
  6.     private static final Logger logger = Logger.getLogger(LogTraceAdvice.class);  
  7.     public void before(Method method, Object[] args, Object target)  
  8.             throws Throwable {  
  9.         // 获得执行的方法名字  
  10.         String methodName = method.getName();  
  11.         // 检查是否有Log注解,如果没有这个注解就直接返回,有这个注解,进行一下操作。  
  12.         if (!method.isAnnotationPresent(Log.class)) {  
  13.             return;  
  14.         }  
  15.         if (methodName.indexOf("update") > -1) {  
  16.             // 获取注解  
  17.             Log log = method.getAnnotation(Log.class);  
  18.             // 执行日志记录  
  19.             if (log != null) {  
  20.                 saveAction("更新", log.name(), log.comments());  
  21.             } else {  
  22.                 saveAction("更新", "", "");  
  23.             }  
  24.         } else if (methodName.indexOf("insert") > -1) {  
  25.             Log log = method.getAnnotation(Log.class);  
  26.             if (log != null) {  
  27.                 saveAction("新增", log.name(), log.comments());  
  28.             } else {  
  29.                 saveAction("新增", "", "");  
  30.             }  
  31.         } else if (methodName.indexOf("del") > -1) {  
  32.             Log log = method.getAnnotation(Log.class);  
  33.             if (log != null) {  
  34.                 saveAction("删除", log.name(), log.comments());  
  35.             } else {  
  36.                 saveAction("删除", "", "");  
  37.             }  
  38.         }  
  39.     }  
  40.     private void saveAction(String type, String menu, String comments) {  
  41.         logger.info("保存到数据库!" + type + ":" + menu + ":" + comments);  
  42.     }  
  43. }  

 配置文件:

Java代码  

SpringAop在项目中的一些巧妙使用(二)--记录日志
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="  
  6.         http://www.springframework.org/schema/beans  
  7.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  8.         http://www.springframework.org/schema/context  
  9.         http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  10.     <bean name="logAdvice" class="net.zoneland.test.common.annotation.LogTraceAdvice"></bean>  
  11.     <bean name="logProxy"  
  12.         class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
  13.         <property name="interceptorNames">  
  14.             <list>  
  15.                 <value>logAdvice</value>  
  16.             </list>  
  17.         </property>  
  18.         <property name="beanNames">  
  19.             <list>  
  20.                 <value>*Mapper</value>  
  21.             </list>  
  22.         </property>  
  23.     </bean>  
  24. </beans>  

 日志记录完成!