每个请求都会对应一个线程,ThreadLocal就是这个线程使用过程中的一个变量,该变量为其所属线程所有,各个线程互不影响
保存用户信息的工具类
package com.ahies.zgstm.util;
import java.util.HashMap;
import java.util.Map;
/**
* 用于在应用内及应用间保存、传递执行上下文信息。
*
* @author LIU Fangran
*/
public class ExecutionContext {
/**
* 用于保存线程相关信息
*/
transient static ThreadLocal<Map<String, String>> threadLocal = new ThreadLocal<Map<String, String>>();
public static final String USER_ID = "userId";
public static final String USER_NAME = "userName";
public static final String LOGIN_NAME = "loginName";
public static final String DEPT_ID = "deptId";
public static final String TOKEN = "token";
// session类型
public static final String SESSION_TYPE = "sessionType";
public static final String DEVELOPER = "developer";
/**
* 构造函数
*/
public ExecutionContext() {
}
/**
* 从 ThreadLocal中获取名值Map(不包含appCode)
*
* @return 名值Map
*/
public static Map<String, String> getContextMap() {
Map<String, String> map = threadLocal.get();
return map;
}
/**
* 从 ThreadLocal 获取名值Map
*
* @param contextMap 名值Map
*/
public static void setContextMap(Map<String, String> contextMap) {
threadLocal.set(contextMap);
}
/**
* (获取键下的值.如果不存在,返回null;如果名值Map未初始化,也返回null) Get the value of key. Would
* return null if context map hasn't been initialized.
*
* @param key 键
* @return 键下的值
*/
public static String get(String key) {
Map<String, String> contextMap = getContextMap();
if (contextMap == null) {
return null;
}
return contextMap.get(key);
}
/**
* (设置名值对。如果Map之前为null,则会被初始化) Put the key-value into the context map;
* <p>
* Initialize the map if the it doesn't exist.
*
* @param key 键
* @param value 值
* @return 之前的值
*/
public static String put(String key, String value) {
Map<String, String> contextMap = getContextMap();
if (contextMap == null) {
c