读书人

SpringAop在项目中的一些巧妙施用(二

发布时间: 2013-01-18 10:22:42 作者: rapoo

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

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

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

package net.zoneland.test.common.dal;import net.zoneland.test.common.annotation.Log;/** *  * @author wangyong * @version $Id: TestMapper.java, v 0.1 2012-12-27 下午9:13:16 wangyong Exp $ */public interface TestMapper {@Log(name = "某某的维护或者配置", comments = "更新。。。。")public int updateByPrimaryKey();@Log(name = "某某的维护或者配置", comments = "增加。。。。")public void insert();@Log(name = "某某的维护或者配置", comments = "删除。。。。")public int deleteByPrimaryKey();}

?对方法进行注释:

package net.zoneland.test.common.dal;import net.zoneland.ums.common.util.annotation.Log;/** *  * @author wangyong * @version $Id: TestMapper.java, v 0.1 2012-12-27 下午9:13:16 wangyong Exp $ */public interface TestMapper {@Log(name = "某某的维护或者配置", comments = "更新。。。。")public int updateByPrimaryKey();@Log(name = "某某的维护或者配置", comments = "增加。。。。")public void insert();@Log(name = "某某的维护或者配置", comments = "删除。。。。")public int deleteByPrimaryKey();}

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

?

package net.zoneland.test.common.annotation;import java.lang.reflect.Method;import org.apache.log4j.Logger;import org.springframework.aop.MethodBeforeAdvice;/** * 记录日志的方法,与(一)里的记录方法操作时间是一样的,这里是在方法执行前操作。</br> 实现MethodBeforeAdvice *  * @author wangyong * @version $Id: LogTraceAdvice.java, v 0.1 2012-9-6 下午7:57:50 wangyong Exp $ */public class LogTraceAdvice implements MethodBeforeAdvice {private static final Logger logger = Logger.getLogger(LogTraceAdvice.class);/** * @see org.springframework.aop.MethodBeforeAdvice#before(java.lang.reflect.Method, *      java.lang.Object[], java.lang.Object) */public void before(Method method, Object[] args, Object target)throws Throwable {// 获得执行的方法名字String methodName = method.getName();// 检查是否有Log注解,如果没有这个注解就直接返回,有这个注解,进行一下操作。if (!method.isAnnotationPresent(Log.class)) {return;}if (methodName.indexOf("update") > -1) {// 获取注解Log log = method.getAnnotation(Log.class);// 执行日志记录if (log != null) {saveAction("更新", log.name(), log.comments());} else {saveAction("更新", "", "");}} else if (methodName.indexOf("insert") > -1) {Log log = method.getAnnotation(Log.class);if (log != null) {saveAction("新增", log.name(), log.comments());} else {saveAction("新增", "", "");}} else if (methodName.indexOf("del") > -1) {Log log = method.getAnnotation(Log.class);if (log != null) {saveAction("删除", log.name(), log.comments());} else {saveAction("删除", "", "");}}}private void saveAction(String type, String menu, String comments) {logger.info("保存到数据库!" + type + ":" + menu + ":" + comments);}}

?配置文件:

?

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="        http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context-3.0.xsd"><bean name="logAdvice" alt="SpringAop在项目中的一些巧妙施用(二)-记录日志">

?

?

读书人网 >编程

热点推荐