63 Star 427 Fork 156

huifer/Code-Analysis

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Spring-SimpleThreadScope.md 2.06 KB
一键复制 编辑 原始数据 按行查看 历史
huifer 提交于 5年前 . :bookmark:spring scope 接口分析

Spring SimpleThreadScope

  • 类全路径: org.springframework.context.support.SimpleThreadScope

内部变量

private final ThreadLocal<Map<String, Object>> threadScope =
      new NamedThreadLocal<Map<String, Object>>("SimpleThreadScope") {
         @Override
         protected Map<String, Object> initialValue() {
            return new HashMap<>();
         }
      };
  • 内部变量threadScope 是ThreadLocal 中存储了 Map 的这么一个对象

方法列表

get

  • 逻辑
  1. 从ThreadLocal中获取容器
    1. 容器中获取实例
      1. 实例不存在
        1. objectFactory创建
        2. 设置到容器
@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
   // ThreadLocal 中获取容器
   Map<String, Object> scope = this.threadScope.get();
   // 容器中获取
   Object scopedObject = scope.get(name);
   // 不存在
   if (scopedObject == null) {
      // 从 ObjectFactory 中获取
      scopedObject = objectFactory.getObject();
      // 设置到容器
      scope.put(name, scopedObject);
   }
   return scopedObject;
}

remove

@Override
@Nullable
public Object remove(String name) {
   Map<String, Object> scope = this.threadScope.get();
   return scope.remove(name);
}

getConversationId

  • 当前线程的name
@Override
public String getConversationId() {
   return Thread.currentThread().getName();
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/pychfarm_admin/code-analysis.git
git@gitee.com:pychfarm_admin/code-analysis.git
pychfarm_admin
code-analysis
Code-Analysis
v0.0.15

搜索帮助