//ThreadLocal线程封闭 public class RequestHolder { private final static ThreadLocal<Long> requestHolder = new ThreadLocal<Long>(); public static void add(Long id){ requestHolder.set(id); } public static Long getId(){ //自动获取key(当前线程对象地址),找出value值返回 return requestHolder.get(); } public static void remove(){ //数据会一直存到项目重启 //数据一定要移除,避免产生内存溢出(oom) requestHolder.remove(); } }