转载自:http://743389831.iteye.com/blog/1755631
在(一)中我们对AOP加动态代理有了初步认识,那如何使用这个记录用户进行了哪些操作呢?我们已经知道,AOP加动态代理我们可以知道用户都做了什么调用了哪些方法。我们也知道这些方法是干嘛用的,难道我们要写一个代码if调用了这个类的某某方法,那么这个用户做了什么什么....这个明显太复杂。如果我们对一些方法加注释,并且能获得这个注释,我们是不是就把问题解决了呢?那怎样的注释是我们代码能获得呢?解决了这个问题就差不多把问题解决了。
首先你进行注释,使用注解注释,第一步生成注解。
Java代码
- package net.zoneland.test.common.dal;
- import net.zoneland.test.common.annotation.Log;
- public interface TestMapper {
- @Log(name = "某某的维护或者配置", comments = "更新。。。。")
- public int updateByPrimaryKey();
- @Log(name = "某某的维护或者配置", comments = "增加。。。。")
- public void insert();
- @Log(name = "某某的维护或者配置", comments = "删除。。。。")
- public int deleteByPrimaryKey();
- }
对方法进行注释:
Java代码
- package net.zoneland.test.common.dal;
- import net.zoneland.ums.common.util.annotation.Log;
- public interface TestMapper {
- @Log(name = "某某的维护或者配置", comments = "更新。。。。")
- public int updateByPrimaryKey();
- @Log(name = "某某的维护或者配置", comments = "增加。。。。")
- public void insert();
- @Log(name = "某某的维护或者配置", comments = "删除。。。。")
- public int deleteByPrimaryKey();
- }
然后进行监控操作跟上面记录方法执行时间差不多。
Java代码
- package net.zoneland.test.common.annotation;
- import java.lang.reflect.Method;
- import org.apache.log4j.Logger;
- import org.springframework.aop.MethodBeforeAdvice;
- public class LogTraceAdvice implements MethodBeforeAdvice {
- private static final Logger logger = Logger.getLogger(LogTraceAdvice.class);
- 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);
- }
- }
配置文件:
Java代码
- <?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" class="net.zoneland.test.common.annotation.LogTraceAdvice"></bean>
- <bean name="logProxy"
- class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
- <property name="interceptorNames">
- <list>
- <value>logAdvice</value>
- </list>
- </property>
- <property name="beanNames">
- <list>
- <value>*Mapper</value>
- </list>
- </property>
- </bean>
- </beans>
日志记录完成!