首先寫好一個工具類 LogAspect.java
- package com.yangjf.commons;
- import java.lang.reflect.Method;
- import java.util.Date;
- 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 com.yangjf.entity.Admin;
- import com.yangjf.entity.Log;
- import com.yangjf.service.LogService;
- @Aspect
- public class LogAspect {
- public Integer id=null;
- @Autowired
- LogService logService;
- /**
- * 管理員登錄方法的切入點
- */
- @Pointcut("execution(* com.yangjf.service.*.login(..))")
- public void loginCell(){
- }
- /**
- * 添加業務邏輯方法切入點
- */
- @Pointcut("execution(* com.yangjf.service.*.save(..))")
- public void insertCell() {
- }
- /**
- * 修改業務邏輯方法切入點
- */
- @Pointcut("execution(* com.yangjf.service.*.update(..))")
- public void updateCell() {
- }
- /**
- * 刪除業務邏輯方法切入點
- */
- @Pointcut("execution(* com.yangjf.service.*.delete(..))")
- public void deleteCell() {
- }
- /**
- * 登錄操作(后置通知)
- * @param joinPoint
- * @param object
- * @throws Throwable
- */
- @AfterReturning(value = "loginCell()", argNames = "object", returning = "object")
- public void loginLog(JoinPoint joinPoint, Object object) throws Throwable {
- Admin admin=(Admin)object;
- if (admin==null) {
- return;
- }
- if (joinPoint.getArgs() == null) {// 沒有參數
- return;
- }
- id=admin.getId();
- // 獲取方法名
- String methodName = joinPoint.getSignature().getName();
- // 獲取操作內容
- String opContent = optionContent(joinPoint.getArgs(), methodName);
- Log log = new Log();
- log.setContent(opContent);
- log.setAdminId(admin.getId());
- log.setCreateDate(new Date());
- log.setOperation("登錄");
- logService.insertLog(log);
- }
- /**
- * 添加操作日志(后置通知)
- *
- * @param joinPoint
- * @param object
- */
- @AfterReturning(value = "insertCell()", argNames = "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);;
- log.setOperation("添加");
- log.setCreateDate(new Date());
- logService.insertLog(log);
- }
- /**
- * 管理員修改操作日志(后置通知)
- *
- * @param joinPoint
- * @param object
- * @throws Throwable
- */
- @AfterReturning(value = "updateCell()", argNames = "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);
- log.setOperation("修改");// 操作
- log.setCreateDate(new Date());
- logService.insertLog(log);
- }
- /**
- * 刪除操作
- *
- * @param joinPoint
- * @param object
- */
- @AfterReturning(value = "deleteCell()", argNames = "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] + ")");
- }
- // 創建日志對象
- Log log = new Log();
- log.setContent(rs.toString());
- log.setAdminId(id);
- log.setOperation("刪除");// 操作
- log.setCreateDate(new Date());
- logService.insertLog(log);
- }
- /**
- * 使用Java反射來獲取被攔截方法(insert、update)的參數值, 將參數值拼接為操作內容
- *
- * @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();
- }
- }
aop在applicationcontext.xml的配置
- <!-- 日志 -->
- <aop:aspectj-autoproxy />
- <bean id="logBean" class="com.yangjf.commons.LogAspect"></bean>