赞
踩
- /**
- * 全局上下文工具类,用于储存一些东西
- */
- public class MyContext {
- private static final ThreadLocal<Map<Object, Object>> mycontext = new ThreadLocal<Map<Object, Object>>() {
- @Override
- protected Map<Object, Object> initialValue() {
- return new HashMap<Object, Object>();
- }
-
- };
-
- /**
- * 根据key获取值
- * @param key
- * @return
- */
- public static Object getValue(Object key) {
- if(mycontext.get() == null) {
- return null;
- }
- return mycontext.get().get(key);
- }
-
- /**
- * 存储
- * @param key
- * @param value
- * @return
- */
- public static Object setValue(Object key, Object value) {
- Map<Object, Object> cacheMap = mycontext.get();
- if(cacheMap == null) {
- cacheMap = new HashMap<Object, Object>();
- mycontext.set(cacheMap);
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。