解决 RequestContextHolder 中的参数,同步到子线程中

废话不多说,直接上代码

@Aspect
@Component
@Slf4j
public class RequestThreadAspect {

    /**
     * 切点: 所有配置 TestThread 注解的方法
     */
    @Pointcut("@annotation(com.xiongmaoxingchu.common.DruidLog.ThreadAnnotation)")
    public void dataSourcePointCut() {}

    @Around("dataSourcePointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        ThreadAnnotation ta = method.getAnnotation(ThreadAnnotation.class);
        log.info("子线程共享父线程上线文:{}",ta.value());
        if(ta.value()){
            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            RequestContextHolder.setRequestAttributes(requestAttributes,true);
        }

        return point.proceed();
    }
}

注解代码:

@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface ThreadAnnotation {
    boolean value() default false;
}

 

你可能感兴趣的:(java)