**spring aop 自定义注解方式实现日志管理**
需求:spring boot中请求一些方法时需插入操作日志到日志表中。
原来的处理方式:在每个需要插入日志的方法里封装日志实体然后调用日志保存方法
现在处理方式:spring aop 自定义注解方式实现日志管理
依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
定义切面类:
package com.mmys.crm.config.aop;
import com.alibaba.fastjson.JSON;
import com.mmys.crm.entity.common.OperationLog;
import com.mmys.crm.service.OperationLogDetail;
import com.mmys.crm.service.common.IOperationLogService;
import com.mmys.crm.utils.WebUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* @author EDZ
*/
@Aspect
@Component
public class WebLogAcpect {
private Logger logger = LoggerFactory.getLogger(WebLogAcpect.class);
private IOperationLogService logService;
@Autowired
public void setUserDao (IOperationLogService logService) {
this.logService = logService;
}
/**
* 定义切入点,切入点为加了OperationLogDetail注解的方法
* 也可用execution(public * com.mmys.service..*.save*(..)) || execution(public * com.mmys.service..*.delete*(..))的方式打到相同目的
*/
@Pointcut("@annotation(com.mmys.crm.service.OperationLogDetail)")
public void webLog(){}
/**
* 前置通知:在连接点之前执行的通知
* @param joinPoint
*/
@Before("webLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable{
// 接收到请求,记录请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if(attributes != null){
HttpServletRequest request = attributes.getRequest();
// 记录下请求内容
logger.info("URL : " + request.getRequestURL().toString());
logger.info("HTTP_METHOD : " + request.getMethod());
logger.info("IP : " + request.getRemoteAddr());
logger.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
logger.info("ARGS : " + Arrays.toString(joinPoint.getArgs()));
}
}
/*
* 环绕增强
* @param joinPoint
*/
@Around("webLog()")
public void doAround(ProceedingJoinPoint joinPoint) throws Throwable{
try {
//方法执行完成后增加日志
addOperationLog(joinPoint);
}catch (Exception e){
System.out.println("LogAspect 操作失败:" + e.getMessage());
e.printStackTrace();
}
}
@AfterReturning(returning = "ret",pointcut = "webLog()")
public void doAfterReturning(Object ret) throws Throwable {
// 处理完请求,返回内容
logger.info("RESPONSE : " + ret);
}
@After("webLog()")
public void doAfter(JoinPoint joinPoint) throws Throwable{
logger.info("方法执行完毕");
}
/**
* 操作日志对象
* @param joinPoint
*/
private void addOperationLog(JoinPoint joinPoint){
MethodSignature signature = (MethodSignature)joinPoint.getSignature();
OperationLogDetail annotation = signature.getMethod().getAnnotation(OperationLogDetail.class);
if(annotation != null){
OperationLog log = new OperationLog();
log.setUserId(WebUtils.loginUser().getId());
log.setType(annotation.operationType().getValue());
log.setContent(getContent(((MethodSignature)joinPoint.getSignature()).getParameterNames(),joinPoint.getArgs(),annotation));
log.setBusiness(annotation.business());
log.setCreateTime(new Date());
logService.save(log);
}
}
/**
* 对当前登录用户和占位符处理
* @param argNames 方法参数名称数组
* @param args 方法参数数组
* @param annotation 注解信息
* @return 返回处理后的描述
*/
private String getContent(String[] argNames, Object[] args, OperationLogDetail annotation){
Map<Object, Object> map = new HashMap<>(4);
for(int i = 0;i < argNames.length;i++){
map.put(argNames[i],args[i]);
}
String content = annotation.content();
try {
for (Map.Entry<Object, Object> entry : map.entrySet()) {
Object k = entry.getKey();
Object v = entry.getValue();
content = content.replace("{" + k + "}", JSON.toJSONString(v));
}
}catch (Exception e){
e.printStackTrace();
}
return content;
}
}
service层添加OperationLogDetail 注解
/**
* 添加OperationLogDetail注解
*/
@OperationLogDetail(business = "款项合并",operationType = LogType.UPDATE,content = "合并的款项id:{ids}")
@Override
public String saveMergeMoney(List<Long> ids) {
String returnMsg = "";
return ReturnVO.error(returnMsg);
}
自定义注解:
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface OperationLogDetail {
String business() default "";
/**
* 方法描述,可使用占位符获取参数:{{tel}}
*/
String content() default "";
/**
* 操作类型(enum):主要是select,insert,update,delete
*/
LogType operationType() default LogType.INSERT;
}