权限这ThreadLocal解决高并发(重要)

package com.mall.common;

import com.mall.model.SysUser;

import javax.servlet.http.HttpServletRequest;

/**
 * 用ThreadLocal解决高并发的问题
 */
public class RequestHolder {

    private static final ThreadLocal userHolder = new ThreadLocal();

    private static final ThreadLocal requestHolder = new ThreadLocal();

    public static void add(SysUser sysUser) {
        userHolder.set(sysUser);
    }

    public static void add(HttpServletRequest request) {
        requestHolder.set(request);
    }

    public static SysUser getCurrentUser() {
        return userHolder.get();
    }

    public static HttpServletRequest getCurrentRequest() {
        return requestHolder.get();
    }

    public static void remove() {
        userHolder.remove();
        requestHolder.remove();
    }
}

 

你可能感兴趣的:(java)