1. 程式人生 > >Spring Aop實現使用者操作日誌記錄

Spring Aop實現使用者操作日誌記錄

package com.jixi.controller;
import com.jixi.pojo.Log;
import com.jixi.pojo.User;
import com.jixi.service.ILogService;
import com.jixi.service.IUserService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut
; import org.springframework.beans.factory.annotation.Autowired; import java.lang.reflect.Method; import java.util.Date; @Aspect public class LogAspect { public Integer id=null; @Autowired private ILogService logService; @Autowired private IUserService userService; /** * 管理員登入方法的切入點 */ @Pointcut
("execution(* com.jixi.service.impl.*.selectByUserName(..))") public void loginCell(){} /** * 新增業務邏輯方法切入點 */ @Pointcut("execution(* com.jixi.service.impl.*.add*(..))") public void insertCell() {} /** * 修改業務邏輯方法切入點 */ @Pointcut("execution(* com.jixi.service.impl.*.update*(..))"
) public void updateCell() {} /** * 刪除業務邏輯方法切入點 */ @Pointcut("execution(* com.jixi.service.impl.*.delete*(..))") public void deleteCell() {} /** * 匯入業務邏輯方法切入點 */ @Pointcut("execution(* com.jixi.service.impl.*.import*(..))") public void importCell() {} /** * 匯出業務邏輯方法切入點 */ @Pointcut("execution(* com.jixi.service.impl.*.export*(..))") public void exportCell() {} /** * 登入操作(後置通知) * @param joinPoint * @param object * @throws Throwable */ @AfterReturning(value = "loginCell()", argNames = "joinPoint,object", returning = "object") public void loginLog(JoinPoint joinPoint, Object object) throws Throwable { User user=(User)object; if (user==null) { return; } if (joinPoint.getArgs() == null) {// 沒有引數 return; } id=user.getId(); // 獲取方法名 String methodName = joinPoint.getSignature().getName(); // 獲取操作內容 String opContent = optionContent(joinPoint.getArgs(), methodName); Log log = new Log(); log.setContent(opContent); log.setAdminid(user.getId().toString()); log.setAdmin(user.getUsername()); log.setCreatedate(new Date()); log.setOperation("登入"); logService.saveLog(log); } /** * 新增操作日誌(後置通知) * * @param joinPoint * @param object */ @AfterReturning(value = "insertCell()", argNames = "joinPoint,object", returning = "object") public void insertLog(JoinPoint joinPoint, Object object) throws Throwable { // Admin admin=(Admin) // request.getSession().getAttribute("businessAdmin"); // 判斷引數 if (joinPoint.getArgs() == null) {// 沒有引數 return; } // 獲取方法名 String methodName = joinPoint.getSignature().getName(); // 獲取操作內容 String opContent = optionContent(joinPoint.getArgs(), methodName); Log log = new Log(); log.setContent(opContent); log.setAdminid(id.toString()); log.setAdmin(userService.selectOne(id).getUsername()); log.setOperation("新增"+getMethodChineseName(methodName)); log.setCreatedate(new Date()); logService.saveLog(log); } /** * 管理員修改操作日誌(後置通知) * * @param joinPoint * @param object * @throws Throwable */ @AfterReturning(value = "updateCell()", argNames = "joinPoint,object", returning = "object") public void updateLog(JoinPoint joinPoint, Object object) throws Throwable { // Admin admin=(Admin) // request.getSession().getAttribute("businessAdmin"); // 判斷引數 if (joinPoint.getArgs() == null) {// 沒有引數 return; } // 獲取方法名 String methodName = joinPoint.getSignature().getName(); // 獲取操作內容 String opContent = optionContent(joinPoint.getArgs(), methodName); // 建立日誌物件 Log log = new Log(); log.setContent(opContent); log.setAdminid(id.toString()); log.setAdmin(userService.selectOne(id).getUsername()); log.setOperation("修改"+getMethodChineseName(methodName));// 操作 log.setCreatedate(new Date()); logService.saveLog(log); } /** * 管理員匯入操作日誌(後置通知) * * @param joinPoint * @param object * @throws Throwable */ @AfterReturning(value = "importCell()", argNames = "joinPoint,object", returning = "object") public void importLog(JoinPoint joinPoint, Object object) throws Throwable { // Admin admin=(Admin) // request.getSession().getAttribute("businessAdmin"); // 判斷引數 if (joinPoint.getArgs() == null) {// 沒有引數 return; } // 獲取方法名 String methodName = joinPoint.getSignature().getName(); // 獲取操作內容 String opContent = optionContent(joinPoint.getArgs(), methodName); // 建立日誌物件 Log log = new Log(); log.setContent(opContent); log.setAdminid(id.toString()); log.setAdmin(userService.selectOne(id).getUsername()); log.setOperation("匯入"+getMethodChineseName(methodName));// 操作 log.setCreatedate(new Date()); logService.saveLog(log); } /** * 管理員匯出操作日誌(後置通知) * * @param joinPoint * @param object * @throws Throwable */ @AfterReturning(value = "exportCell()", argNames = "joinPoint,object", returning = "object") public void exportLog(JoinPoint joinPoint, Object object) throws Throwable { // Admin admin=(Admin) // request.getSession().getAttribute("businessAdmin"); // 判斷引數 if (joinPoint.getArgs() == null) {// 沒有引數 return; } // 獲取方法名 String methodName = joinPoint.getSignature().getName(); // 獲取操作內容 String opContent = optionContent(joinPoint.getArgs(), methodName); // 建立日誌物件 Log log = new Log(); log.setContent(opContent); log.setAdminid(id.toString()); log.setAdmin(userService.selectOne(id).getUsername()); log.setOperation("匯出"+getMethodChineseName(methodName));// 操作 log.setCreatedate(new Date()); logService.saveLog(log); } /** * 刪除操作 * * @param joinPoint * @param object */ @AfterReturning(value = "deleteCell()", argNames = "joinPoint,object", returning = "object") public void deleteLog(JoinPoint joinPoint, Object object) throws Throwable { // Admin admin=(Admin) // request.getSession().getAttribute("businessAdmin"); // 判斷引數 if (joinPoint.getArgs() == null) {// 沒有引數 return; } // 獲取方法名 String methodName = joinPoint.getSignature().getName(); StringBuffer rs = new StringBuffer(); rs.append(methodName); String className = null; for (Object info : joinPoint.getArgs()) { // 獲取物件型別 className = info.getClass().getName(); className = className.substring(className.lastIndexOf(".") + 1); rs.append("[引數,型別:" + className + ",值:(id:" + joinPoint.getArgs()[0] + ")"); } rs.append("]"); // 建立日誌物件 Log log = new Log(); log.setContent(rs.toString()); log.setAdminid(id.toString()); log.setAdmin(userService.selectOne(id).getUsername()); log.setOperation("刪除"+getMethodChineseName(methodName));// 操作 log.setCreatedate(new Date()); logService.saveLog(log); } /** * 使用Java反射來獲取被攔截方法(insertupdate)的引數值, 將引數值拼接為操作內容 * * @param args * @param mName * @return */ public String optionContent(Object[] args, String mName) { if (args == null) { return null; } StringBuffer rs = new StringBuffer(); rs.append(mName); String className = null; int index = 1; // 遍歷引數物件 for (Object info : args) { // 獲取物件型別 className = info.getClass().getName(); className = className.substring(className.lastIndexOf(".") + 1); rs.append("[引數" + index + ",型別:" + className + ",值:"); // 獲取物件的所有方法 Method[] methods = info.getClass().getDeclaredMethods(); // 遍歷方法,判斷get方法 for (Method method : methods) { String methodName = method.getName(); // 判斷是不是get方法 if (methodName.indexOf("get") == -1) {// 不是get方法 continue;// 不處理 } Object rsValue = null; try { // 呼叫get方法,獲取返回值 rsValue = method.invoke(info); } catch (Exception e) { continue; } // 將值加入內容中 rs.append("(" + methodName + ":" + rsValue + ")"); } rs.append("]"); index++; } return rs.toString(); } /** * 判斷操作的中文名(根據自己專案而定) * @param methodName * @return */ public String getMethodChineseName(String methodName){ if(methodName.endsWith("Allorder")){ return "通用訂單分析資料"; }else if(methodName.endsWith("Commission")){ return "業務員提成比例"; }else if(methodName.endsWith("CustomerDetail")){ return "客戶月度明細資料"; }else if(methodName.endsWith("Customer")){ return "客戶資訊"; }else if(methodName.endsWith("CustomerTypeDetail")){ return "客戶型別月度明細資料"; }else if(methodName.endsWith("ItemBuy")){ return "商品入庫資料"; }else if(methodName.endsWith("ItemSell")){ return "商品出庫資料"; }else if(methodName.endsWith("Item")){ return "商品資料"; }else if(methodName.endsWith("Log")){ return "系統日誌資料"; }else if(methodName.endsWith("PickorderItem")){ return "商品售出資料"; }else if(methodName.endsWith("ItemRank")){ return "商品售出排行"; }else if(methodName.endsWith("PickOrderSumAsFinance")){ return "配貨單財務資料"; }else if(methodName.endsWith("PickOrderSum")){ return "配貨單資料"; }else if(methodName.endsWith("SellmanDetail")){ return "銷售月度明細資料"; }else if(methodName.endsWith("Sellman")){ return "銷售資訊"; }else if(methodName.endsWith("SellorderAll")){ return "通用客戶分析資料"; }else if(methodName.endsWith("SellorderSum")){ return "客戶訂單資料"; }else if(methodName.endsWith("SellorderSumAsSellman")){ return "銷售的客戶訂單資料"; }else if(methodName.endsWith("SellorderSumAsFinance")){ return "客戶訂單財務資料"; }else if(methodName.endsWith("SupplierDetail")){ return "供應商月度明細資料"; }else if(methodName.endsWith("Supplier")){ return "供應商資訊"; }else if(methodName.endsWith("Tax")){ return "進銷項資料"; }else if(methodName.endsWith("User")){ return "管理員"; }else { return ""; } } }
【重點】在applicationContext.xml中加入以下aop的配置: