RequestHolder

import com.mmall.model.SysUser;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: zhouheng
 * @Created: with IntelliJ IDEA.
 * @Description:
 * @Date: 2018-07-17
 * @Time: 14:56
 */
public class RequestHolder {

    private static final ThreadLocal USER_THREAD_LOCAL = new ThreadLocal();
    private static final ThreadLocal REQUEST_THREAD_LOCAL = new ThreadLocal<>();

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

    }

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

    }

    public static SysUser getCurrentUser() {
        return USER_THREAD_LOCAL.get();
    }
    public static HttpServletRequest getCurrentRequest() {
        return REQUEST_THREAD_LOCAL.get();
    }

    public static void remove() {
        USER_THREAD_LOCAL.remove();
        REQUEST_THREAD_LOCAL.remove();

    }
}

 

你可能感兴趣的:(javaEE)