使用ThreadLocal存储用户信息,方便全局获取。
/**
* 使用ThreadLocal存储用户信息,方便全局获取
*/
public class UserInfoThreadLocal {
private UserInfoThreadLocal() {
}
public static final ThreadLocal<User> USER_THREAD_LOCAL = new ThreadLocal<>();
public static void setUser(User user) {
USER_THREAD_LOCAL.set(user);
}
public static User getUser() {
User user = USER_THREAD_LOCAL.get();
return user;
}
public static void clear() {
USER_THREAD_LOCAL.remove();
}
}