top.doudou.common.aop.LogDataAspect Maven / Gradle / Ivy
package top.doudou.common.aop;
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.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import top.doudou.common.tool.config.entity.ConfigConstant;
import top.doudou.common.tool.random.RandomUtils;
import top.doudou.common.tool.utils.ServletUtils;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
/**
* @Description 切面处理器
* @Author 傻男人 <[email protected]>
* @Date 2020-09-24 14:53
* @Version V1.0
*/
@ComponentScan
@Component
@Aspect
@EnableAspectJAutoProxy(exposeProxy = true)
public class LogDataAspect {
@Resource
private AopLogProcessor aopLogProcessor;
/**
* 将会切 被AopLog注解标记的方法
*/
@Pointcut("@annotation(top.doudou.common.aop.AopLog) || @within(top.doudou.common.aop.AopLog)")
public void aopLogPointCut() {
//ig
}
@Around("aopLogPointCut()")
public Object note(ProceedingJoinPoint point) throws Throwable {
return aopLog(point);
}
/**
* @param point aop 切点对象
* @return 返回执行结果
* @throws Throwable Exceptions in AOP should be thrown out and left to the specific business to handle
*/
private Object aopLog(ProceedingJoinPoint point) throws Throwable {
try {
HttpServletRequest request = ServletUtils.getRequest();
LogData.removeCurrent();
LogData data = LogData.getCurrent();
Object requestCode = request.getAttribute(ConfigConstant.REQUEST_UUID);
if(null == requestCode ){
String code = RandomUtils.randomUUID(15);
request.setAttribute(ConfigConstant.REQUEST_UUID, code);
data.setReqCode(code);
}else {
data.setReqCode(requestCode.toString());
}
return aopLogProcessor.proceed(data, point);
} finally {
LogData.removeCurrent();
}
}
}