threadLocal 工具类 用于存放用户id的容器


/**
 * 存放用户id的容器
 */
public class AuthInfoHolder {

    private final static ThreadLocal<AuthInfo> threadLocal = new ThreadLocal<>();

    /**
     * 获取线程中的用户
     */
    public static AuthInfo getAuthInfo() {
        return threadLocal.get();
    }

    /**
     * 设置当前线程中的用户
     */
    public static void setAuthInfo(AuthInfo info) {
        threadLocal.set(info);
    }

    public static Long getUserId() {
        return threadLocal.get().getUid();
    }

    public static Long getCompanyId() {
        if(threadLocal.get() != null) {
            return threadLocal.get().getCompanyId();
        }else {
            return null;
        }

    }

    public static void remove(){

        threadLocal.remove();
    }

}

你可能感兴趣的:(实用工具类(宝藏级收藏),java,开发语言)