@Configuration
@Aspect
public class LogAop {
@Autowired
private LogMapper logMapper;
//自动装配request
@Autowired
private HttpServletRequest request;
//日志对象
private Logger logger = LoggerFactory.getLogger(LogAop.class);
//注解切入点
@Pointcut("@annotation(com.baizhi.annotation.Log)")
public void logPoint(){
}
/**
* joinPoint 连接点对象
* @param joinPoint
*/
@After(" logPoint()")
public void logInsert(JoinPoint joinPoint){
//获得方法签名,通过签名获得该连接点的方法
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
//通过该方法获得标注的自定义注解
Log annotation = method.getAnnotation(Log.class);
String value = annotation.value();
logger.debug(value);
//装填数据
com.baizhi.entity.Log log = new com.baizhi.entity.Log();
log.setLogDate(new Date());
log.setLogContent(value);
log.setLogIp("");
//自动装配HttpServletRequest 获得session
HttpSession session = request.getSession();
Admin admin = (Admin) session.getAttribute("admin");
log.setAdminId(admin.getId());
//日志数据 持久化到数据库
logMapper.insert(log);
}
}
SpringBoot-注解日志 Aop注解切入点
最新推荐文章于 2024-10-12 09:25:55 发布