Recently, the project needs to make a logging function, which I use Spring Aop annotation method to implement.
Create log annotations
package com.wyj.annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** * Log annotation* * * @author: WangYuanJun * @date: August 26, 2016 at 8:25:35 pm */@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface SysLog { String action() default "";//action}Create a facet notification class
Record the method name, parameters and time spent on the operation, using surround notifications
package com.wyj.aspect;import java.lang.reflect.Method;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Pointcut;import org.aspectj.lang.reflect.MethodSignature;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import com.wyj.annotation.SysLog;import com.wyj.entity.SysLogEntity;import com.wyj.service.SysLogService;/** * Log Section Notification* * * @author: WangYuanJun * @date: August 26, 2016 at 10:28:57 pm */@Aspect@Componentpublic class SysLogAspect { @Autowired private SysLogService sysLogService; /** * Point of entry*/ @Pointcut("@annotation(com.wyj.annotation.SysLog)") public void pointCut() {} /** * Surround notification* * @param joinPoint * @return * @throws Throwable */ @Around("pointCut()") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { // Start time long beginTime = System.currentTimeMillis(); // Execute the target method Object result = joinPoint.proceed(); // Execution time (milliseconds) long time = System.currentTimeMillis() - beginTime; // Save the log saveSysLog(joinPoint, time); return result; } /** * Save the log* * @param joinPoint * @param time */ private void saveSysLog(ProceedingJoinPoint joinPoint, long time) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); SysLogEntity sysLogEntity = new SysLogEntity(); SysLog sysLog = method.getAnnotation(SysLog.class); if (sysLog != null) { // Description on the annotation sysLogEntity.setOperation(sysLog.action()); } // Get the target className = joinPoint.getTarget().getClass().getName(); // Get the method name String methodName = signature.getName(); sysLogEntity.setMethod(className + "." + methodName + "()"); // Requested parameter Object[] args = joinPoint.getArgs(); if (args != null && args.length != 0 && args[0] != null) { sysLogEntity.setParams(args[0].toString()); } sysLogEntity.setTime(time); // Save the system log sysLogService.save(sysLogEntity); }}Scan and start aop annotation
Application of log annotation
Effect
The above Spring Aop's AspectJ annotation configuration method to implement log management is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.