Aop
- Todo el mundo sabe que la AOP se utiliza principalmente para la supervisión del rendimiento, el registro y otras funciones. Hoy implementaremos la función de registro de operaciones a través de código específico
-
Crear proyecto, dependencias de importación
-
Crear base de datos
CREATE TABLE sysLog( id VARCHAR2(32) default SYS_GUID() PRIMARY KEY, - primary key, meaningless visitTime timestamp, - visit time username VARCHAR2(50), - username ip VARCHAR2(30), -- ip url VARCHAR2(50), -- url executionTime int, - the execution time of a method method VARCHAR2(200) - method of execution )
-
Clase de modelo
package com.junyang.ssm.domain; import java.util.Date; /** * System logging model */ public class SysLog { private String id; private Date visitTime; private String visitTimeStr; private String username; private String ip; private String url; private Long executionTime; private String method; public String getId() { return id; } public void setId(String id) { this.id = id; } public Date getVisitTime() { return visitTime; } public void setVisitTime(Date visitTime) { this.visitTime = visitTime; } public String getVisitTimeStr() { return visitTimeStr; } public void setVisitTimeStr(String visitTimeStr) { this.visitTimeStr = visitTimeStr; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public Long getExecutionTime() { return executionTime; } public void setExecutionTime(Long executionTime) { this.executionTime = executionTime; } public String getMethod() { return method; } public void setMethod(String method) { this.method = method; } }
-
Código AOP
package com.junyang.ssm.web; import com.junyang.ssm.domain.SysLog; import com.junyang.ssm.service.SysLogService; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.context.SecurityContext; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.User; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; import java.lang.reflect.Method; import java.util.Date; @Component @Aspect public class LogAop { @Autowired private HttpServletRequest request; @Autowired private SysLogService sysLogService; private Date visitTime; //Visit time private Class clazz; //Accessed class private Method method; //Access method //Pre-notification @Before("execution(* com.junyang.ssm.web.*.*(..))") public void doBefore(JoinPoint jp) throws NoSuchMethodException { visitTime = new Date(); //The time when the method starts to visit clazz=jp.getTarget().getClass(); //Accessed class String methodName = jp.getSignature().getName(); //Get the name of the access method Object[] args = jp.getArgs(); //Get the parameters of the access method if(args==null||args.length==0){ method = clazz.getMethod(methodName); //Only get methods without parameters }else{ Class[] classArgs = new Class[args.length]; for(int i=0;i<args.length;i++){ classArgs[i]=args[i].getClass(); } method =clazz.getMethod(methodName,classArgs); } } //Post notification @After("execution(* com.junyang.ssm.web.*.*(..))") public void doAfter(JoinPoint jp){ long time = new Date().getTime()-visitTime.getTime(); //The duration of the visit String url = ""; //URL to visit if(clazz!=null&&method!=null&&clazz!=LogAop.class&&clazz!=SysLogController.class){ RequestMapping classAnnotation = (RequestMapping) clazz.getAnnotation(RequestMapping.class); if(classAnnotation!=null){ String[] classValue = classAnnotation.value(); RequestMapping methodAnnotation = method.getAnnotation(RequestMapping.class); if(methodAnnotation!=null){ String[] methodValue = methodAnnotation.value(); url = classValue[0]+methodValue[0]; String ip = request.getRemoteAddr(); SecurityContext context = SecurityContextHolder.getContext(); User user = (User) context.getAuthentication().getPrincipal(); String username = user.getUsername(); SysLog sysLog = new SysLog(); sysLog.setExecutionTime(time); sysLog.setIp(ip); sysLog.setUrl(url); sysLog.setUsername(username); sysLog.setMethod("[class name]"+clazz.getName()+"[method name]"+method.getName()); sysLog.setVisitTime(visitTime); sysLogService.save(sysLog); } } } } }
-
Se omiten los códigos de controlador, servicio y dao. Porque es una operación de inserción normal.
-
.