轉載自: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>
日志記錄完成!