spring aop 实现用户操作日志记录功能(转)
首先写好一个工具类 LogAspect.java
1 package com.yangjf.commons; 2 import java.lang.reflect.Method; 3 import java.util.Date; 4 import org.aspectj.lang.JoinPoint; 5 import org.aspectj.lang.annotation.AfterReturning; 6 import org.aspectj.lang.annotation.Aspect; 7 import org.aspectj.lang.annotation.Pointcut; 8 import org.springframework.beans.factory.annotation.Autowired; 9 import com.yangjf.entity.Admin; 10 import com.yangjf.entity.Log; 11 import com.yangjf.service.LogService; 12 13 @Aspect 14 public class LogAspect { 15 16 public Integer id=null; 17 18 @Autowired 19 LogService logService; 20 21 /** 22 * 管理员登录方法的切入点 23 */ 24 @Pointcut("execution(* com.yangjf.service.*.login(..))") 25 public void loginCell(){ 26 } 27 28 /** 29 * 添加业务逻辑方法切入点 30 */ 31 @Pointcut("execution(* com.yangjf.service.*.save(..))") 32 public void insertCell() { 33 } 34 35 /** 36 * 修改业务逻辑方法切入点 37 */ 38 @Pointcut("execution(* com.yangjf.service.*.update(..))") 39 public void updateCell() { 40 } 41 42 /** 43 * 删除业务逻辑方法切入点 44 */ 45 @Pointcut("execution(* com.yangjf.service.*.delete(..))") 46 public void deleteCell() { 47 } 48 49 /** 50 * 登录操作(后置通知) 51 * @param joinPoint 52 * @param object 53 * @throws Throwable 54 */ 55 @AfterReturning(value = "loginCell()", argNames = "object", returning = "object") 56 public void loginLog(JoinPoint joinPoint, Object object) throws Throwable { 57 Admin admin=(Admin)object; 58 if (admin==null) { 59 return; 60 } 61 if (joinPoint.getArgs() == null) {// 没有参数 62 return; 63 } 64 id=admin.getId(); 65 // 获取方法名 66 String methodName = joinPoint.getSignature().getName(); 67 // 获取操作内容 68 String opContent = optionContent(joinPoint.getArgs(), methodName); 69 70 Log log = new Log(); 71 log.setContent(opContent); 72 log.setAdminId(admin.getId()); 73 log.setCreateDate(new Date()); 74 log.setOperation("登录"); 75 logService.insertLog(log); 76 } 77 78 /** 79 * 添加操作日志(后置通知) 80 * 81 * @param joinPoint 82 * @param object 83 */ 84 @AfterReturning(value = "insertCell()", argNames = "object", returning = "object") 85 public void insertLog(JoinPoint joinPoint, Object object) throws Throwable { 86 // Admin admin=(Admin) 87 // request.getSession().getAttribute("businessAdmin"); 88 // 判断参数 89 if (joinPoint.getArgs() == null) {// 没有参数 90 return; 91 } 92 // 获取方法名 93 String methodName = joinPoint.getSignature().getName(); 94 // 获取操作内容 95 String opContent = optionContent(joinPoint.getArgs(), methodName); 96 97 Log log = new Log(); 98 log.setContent(opContent); 99 log.setAdminId(id);; 100 log.setOperation("添加"); 101 log.setCreateDate(new Date()); 102 logService.insertLog(log); 103 } 104 105 /** 106 * 管理员修改操作日志(后置通知) 107 * 108 * @param joinPoint 109 * @param object 110 * @throws Throwable 111 */ 112 @AfterReturning(value = "updateCell()", argNames = "object", returning = "object") 113 public void updateLog(JoinPoint joinPoint, Object object) throws Throwable { 114 // Admin admin=(Admin) 115 // request.getSession().getAttribute("businessAdmin"); 116 117 // 判断参数 118 if (joinPoint.getArgs() == null) {// 没有参数 119 return; 120 } 121 // 获取方法名 122 String methodName = joinPoint.getSignature().getName(); 123 // 获取操作内容 124 String opContent = optionContent(joinPoint.getArgs(), methodName); 125 126 // 创建日志对象 127 Log log = new Log(); 128 log.setContent(opContent); 129 log.setAdminId(id); 130 log.setOperation("修改");// 操作 131 log.setCreateDate(new Date()); 132 logService.insertLog(log); 133 } 134 135 /** 136 * 删除操作 137 * 138 * @param joinPoint 139 * @param object 140 */ 141 @AfterReturning(value = "deleteCell()", argNames = "object", returning = "object") 142 public void deleteLog(JoinPoint joinPoint, Object object) throws Throwable { 143 // Admin admin=(Admin) 144 // request.getSession().getAttribute("businessAdmin"); 145 // 判断参数 146 if (joinPoint.getArgs() == null) {// 没有参数 147 return; 148 } 149 // 获取方法名 150 String methodName = joinPoint.getSignature().getName(); 151 152 StringBuffer rs = new StringBuffer(); 153 rs.append(methodName); 154 String className = null; 155 for (Object info : joinPoint.getArgs()) { 156 // 获取对象类型 157 className = info.getClass().getName(); 158 className = className.substring(className.lastIndexOf(".") + 1); 159 rs.append("[参数,类型:" + className + ",值:(id:" 160 + joinPoint.getArgs()[0] + ")"); 161 } 162 163 // 创建日志对象 164 Log log = new Log(); 165 log.setContent(rs.toString()); 166 log.setAdminId(id); 167 log.setOperation("删除");// 操作 168 log.setCreateDate(new Date()); 169 logService.insertLog(log); 170 } 171 172 /** 173 * 使用Java反射来获取被拦截方法(insert、update)的参数值, 将参数值拼接为操作内容 174 * 175 * @param args 176 * @param mName 177 * @return 178 */ 179 public String optionContent(Object[] args, String mName) { 180 if (args == null) { 181 return null; 182 } 183 StringBuffer rs = new StringBuffer(); 184 rs.append(mName); 185 String className = null; 186 int index = 1; 187 // 遍历参数对象 188 for (Object info : args) { 189 // 获取对象类型 190 className = info.getClass().getName(); 191 className = className.substring(className.lastIndexOf(".") + 1); 192 rs.append("[参数" + index + ",类型:" + className + ",值:"); 193 // 获取对象的所有方法 194 Method[] methods = info.getClass().getDeclaredMethods(); 195 // 遍历方法,判断get方法 196 for (Method method : methods) { 197 String methodName = method.getName(); 198 // 判断是不是get方法 199 if (methodName.indexOf("get") == -1) {// 不是get方法 200 continue;// 不处理 201 } 202 Object rsValue = null; 203 try { 204 // 调用get方法,获取返回值 205 rsValue = method.invoke(info); 206 } catch (Exception e) { 207 continue; 208 } 209 // 将值加入内容中 210 rs.append("(" + methodName + ":" + rsValue + ")"); 211 } 212 rs.append("]"); 213 index++; 214 } 215 return rs.toString(); 216 } 217 218 }
aop在applicationcontext.xml的配置
1 <!-- 日志 --> 2 <aop:aspectj-autoproxy /> 3 <bean id="logBean" class="com.yangjf.commons.LogAspect"></bean>
原文链接:https://blog.csdn.net/qq_27242475/article/details/51152071#