@Data
public class OptDto {
private String requestIp; //操作IP
private String type; //日志类型 LogType{OPT:操作类型;EX:异常类型}
private String userName; //操作人
private String description; //操作描述
}
public class SysLogEvent extends ApplicationEvent {
public SysLogEvent(OptLogDTO optLogDTO) {
super(optLogDTO);
}
}
@Component
public class SysLogListener {
@Async//异步处理
@EventListener(SysLogEvent.class)
public void saveSysLog(SysLogEvent event) {
OptLogDTO sysLog = (OptLogDTO) event.getSource();
long id = Thread.currentThread().getId();
System.out.println("监听到日志操作事件:" + sysLog + " 线程id:" + id);
//将日志信息保存到数据库...
}
}
@Autowired
private ApplicationContext applicationContext;
@GetMapping("/getUser")
public String getUser(){
//构造操作日志信息
OptLogDTO logInfo = new OptLogDTO();
logInfo.setRequestIp("127.0.0.1");
logInfo.setUserName("admin");
logInfo.setType("OPT");
logInfo.setDescription("查询用户信息");
//构造事件对象
ApplicationEvent event = new SysLogEvent(logInfo);
//发布事件
applicationContext.publishEvent(event);
long id = Thread.currentThread().getId();
System.out.println("发布事件,线程id:" + id);
return "OK";
}
@SpringBootApplication
@EnableAsync//启用异步处理
public class aadStarter {
public static void main(String[] args) {
SpringApplication.run(aadStarter.class,args);
}
}
参考:Spring自定义注解的使用(实现切面
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
@Pointcut("@annotation(com.itheima.pinda.log.annotation.SysLog)")
public void sysLogAspect() {
}
package org.example.Event;
import java.lang.annotation.*;
/**
* 操作日志注解
*
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SysLog {
/**
* 描述
*
* @return {String}
*/
String value() default "";
}
@Before(value = "mySysLogAspect()")
public void doBefore(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Class<?> aClass = joinPoint.getTarget().getClass();
Method method = signature.getMethod();
log.info("before");
System.out.println("before" + method.getName() + aClass.toString());
}
@Around(value = "mySysLogAspect()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
Object[] args = joinPoint.getArgs();
for(Object object: args){
System.out.println(object.toString());
}
Object proceed = joinPoint.proceed();
if(proceed instanceof String){
Object res = (String)proceed + "加了一点东西";
return res;
}
return proceed;
}
aound的before -> before -> 方法执行 -> around 的after->after-> aftreReturn
aound的before -> before -> 方法执行 -> after-> AfterThrowing
package org.example.Event;
import lombok.extern.slf4j.Slf4j;
import net.minidev.json.JSONObject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
@Aspect
@Component
@Slf4j
@Order(0)
public class SysLogAspect {
@Autowired
private ApplicationContext applicationContext;
private static final ThreadLocal<OptLogDTO> THREAD_LOCAL = new ThreadLocal<>();
/***
* 定义controller切入点拦截规则,拦截SysLog注解的方法
*/
@Pointcut("@annotation(org.example.Event.SysLog)")
public void mySysLogAspect() {
}
@Before(value = "mySysLogAspect()")
public void doBefore(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Class<?> aClass = joinPoint.getTarget().getClass();
Method method = signature.getMethod();
//log.info("before");
System.out.println("before" + "______"+method.getName() + aClass.toString());
}
@Around(value = "mySysLogAspect()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
Object[] args = joinPoint.getArgs();
for(Object object: args){
System.out.println(object.toString());
}
Object proceed = joinPoint.proceed();
if(proceed instanceof String){
Object res = (String)proceed + "加了一点东西";
return res;
}
return proceed;
}
// OptLogDTO optLogDTO = THREAD_LOCAL.get();
// optLogDTO.setDescription("准备操作");
// optLogDTO.setRequestIp("127.0.0.1");
// optLogDTO.setType("man");
// optLogDTO.setUserName("jack");
// System.out.println("around");
// THREAD_LOCAL.set(optLogDTO);
// }
@After(value = "mySysLogAspect()")
public void doAfter(JoinPoint joinPoint) throws Throwable {
System.out.println("after");
}
@AfterReturning(value = "mySysLogAspect()")
public void doAfterReturn(JoinPoint joinPoint){
System.out.println("AfterReturning");
}
// @Around(value = "mySysLogAspect()")
// public void doAround(JoinPoint joinPoint){
// System.out.println("around");
// }
@AfterThrowing(value = "mySysLogAspect()")
public void doAfterThrowing(JoinPoint joinPoint){
// System.out.println("success");
// OptLogDTO optLogDTO = THREAD_LOCAL.get();
// applicationContext.publishEvent(new SysLogEvent(optLogDTO));
// THREAD_LOCAL.remove();
System.out.println("AfterThrowing");
}
}
@Around(value = "mySysLogAspect()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
Object[] args = joinPoint.getArgs();
for(Object object: args){
System.out.println(object.toString());
}
Object proceed = joinPoint.proceed();
//构造操作日志信息
OptLogDTO logInfo = new OptLogDTO();
logInfo.setRequestIp("127.0.0.1");
logInfo.setUserName("admin");
logInfo.setType("OPT");
logInfo.setDescription("查询用户信息");
//构造事件对象
ApplicationEvent event = new SysLogEvent(logInfo);
//发布事件
applicationContext.publishEvent(event);
if(proceed instanceof String){
Object res = (String)proceed + "加了一点东西";
return res;
}
return proceed;
}