1. First, customize the annotation class SysLogAnnotation
/** * @date 2019/2/1 Operation log annotation */ @Target() @Retention() @Documented public @interface SysLogAnnotation { /** * describe * @return {String} */ String value(); }
2. Add the section class SysLogAspect to realize the same log addition
1. Use @Aspect annotation to define the facet class
@Component Register bean
@Around(“@annotation(sysLogAnnotation)”)
Implementing the method of adding @sysLogAnnotation annotation to the section surround listening,
Get the value value in the annotation through()
@Aspect @Slf4j @Component public class SysLogAspect { @Around("@annotation(sysLogAnnotation)") @SneakyThrows public Object around(ProceedingJoinPoint point, sysLogAnnotation) { String strClassName = ().getClass().getName(); String strMethodName = ().getName(); ("[Class Name]:{},[method]:{}", strClassName, strMethodName); SysLog logVo = (); (()); // Send asynchronous log events Long startTime = (); Object obj; try { obj = (); } catch (Exception e) { (()); (()); throw e; } finally { Long endTime = (); ((endTime - startTime)); (new SysLogEvent(logVo)); } return obj; } } @Slf4j @UtilityClass public class SysLogUtils { public SysLog getSysLog() { //Get request url,ip,httpMethod HttpServletRequest request = ((ServletRequestAttributes) Objects .requireNonNull(())).getRequest(); SysLog sysLog = new SysLog(); //((getUsername())); //((getUsername())); (()); ((request)); ((())); (()); ((HttpHeaders.USER_AGENT)); ((())); //(getClientId(request)); return sysLog; } }
2. Inherit ApplicationEvent
Custom Event SysLogEvent
public class SysLogEvent extends ApplicationEvent { public SysLogEvent(SysLog source) { super(source); } }
3. Implement the ApplicationContextAware interface
Get a custom context manager
Call (event) method to publish events
When listening to the method in the section, call the event publishing method to publish the event
@Slf4j @Service @Lazy(false) public class SpringContextHolder implements ApplicationContextAware, DisposableBean { private static ApplicationContext applicationContext = null; /** * Get the ApplicationContext stored in the static variable. */ public static ApplicationContext getApplicationContext() { return applicationContext; } /** * Implement the ApplicationContextAware interface and inject Context into static variables. */ @Override public void setApplicationContext(ApplicationContext applicationContext) { = applicationContext; } /** * Get the bean from the static variable applicationContext and automatically transform it into the type of the assigned object. */ @SuppressWarnings("unchecked") public static <T> T getBean(String name) { return (T) (name); } /** * Get the bean from the static variable applicationContext and automatically transform it into the type of the assigned object. */ public static <T> T getBean(Class<T> requiredType) { return (requiredType); } /** * Clear ApplicationContext in SpringContextHolder to Null. */ public static void clearHolder() { if (()) { ("Clear ApplicationContext in SpringContextHolder:" + applicationContext); } applicationContext = null; } /** * Post an event * @param event */ public static void publishEvent(ApplicationEvent event) { if (applicationContext == null) { return; } (event); } /** * Implement the DisposableBean interface to clean static variables when Context is closed. */ @Override @SneakyThrows public void destroy() { (); } }
3. Write class monitor events
Specify the listening time through @EventListener() annotation
@Async annotation asynchronous execution event
@RequiredArgsConstructor annotation can be written on the class instead of @AutoWired annotation
It should be noted that you need to use final definition during injection, or use @notnull annotation
@Slf4j @RequiredArgsConstructor @Component public class SysLogListener { private final SysLogMapper sysLogMapper; @Async @Order @EventListener() public void saveSysLog(SysLogEvent event) { SysLog sysLog = (SysLog) (); (sysLog); } }
4. Methods in controller
Adding @SysLogAnnotation annotation can enable the addition of logs through aop sections
@SysLogAnnotation("Export log") @ApiOperation(value = "Export Test",notes = "Export Test") @GetMapping("export") public Object exportTest() { (); return "success"; }
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.