为每一个请求线程 Thread-per-Request 保存当前请求的用户信息userInfo。
在程序的任何地点 拿到的userInfo 都是 同一个请求用户。
需要将 用户信息保存在 线程私有的 ThreadLocal中。 在并发情况下不会产生 线程同步问题。
public class UserInfo { private static InheritableThreadLocal<User> tokenPool = new InheritableThreadLocal<User>(); public static User get() { return tokenPool.get(); } public static void set(User user) { if (get() == null) { tokenPool.set(user); } } public static void remove() { if (get() != null) { tokenPool.remove(); } } }