63 Star 428 Fork 156

huifer/Code-Analysis

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
docs
beans
AbstractNestablePropertyAccessor
BeanDefinition
BeanInfoFactory
BeanMetadataElement
BeanWrapper
CachedIntrospectionResults
ComponentDefinition
ConfigurablePropertyAccessor
GenericTypeAwarePropertyDescriptor
Property
PropertyAccessor
Scope
images
Readme.md
Spring-AbstractRequestAttributesScope.md
Spring-Scope.md
Spring-ServletContextScope.md
Spring-SimpSessionScope.md
Spring-SimpleMapScope.md
Spring-SimpleThreadScope.md
Spring-SimpleTransactionScope.md
factory
propertyEditor
register
core
env
other
utils
release
.gitattributes
.gitignore
CHANGELOG.MD
LICENSE
Notice
README.md
_coverpage.md
index.html
summary.md
todo.md
克隆/下载
Spring-SimpSessionScope.md 3.69 KB
一键复制 编辑 原始数据 按行查看 历史
huifer 提交于 5年前 . :bookmark:spring scope 接口分析

Spring SimpSessionScope

  • 类全路径: org.springframework.messaging.simp.SimpSessionScope

SimpSessionScope 没有成员变量

其操作对象是通过 SimpAttributesContextHolder 获取的得到的SimpAttributes

这里简单描述一下 SimpAttributesContextHolderSimpAttributes

SimpAttributesContextHolder 中存放了一个 ThreadLocal 存储SimpAttributes

image-20201106130235350

SimpAttributes使用到的字段如下

image-20201106130253794

在了解上述内容后进行方法分析

方法分析

get

  • 通过SimpAttributesContextHolder 虎丘属性表
    • 从属性表中获取 name 对应的实体
      • 不存在 通过object factory 创建
        • 放入属性表
@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
   // 获取当前的属性
   SimpAttributes simpAttributes = SimpAttributesContextHolder.currentAttributes();
   // 从属性表中获取对象
   Object scopedObject = simpAttributes.getAttribute(name);
   if (scopedObject != null) {
      return scopedObject;
   }
   // 处理线程问题
   synchronized (simpAttributes.getSessionMutex()) {
      // 从属性表中获取对象
      scopedObject = simpAttributes.getAttribute(name);
      if (scopedObject == null) {
         // 通过 object factory 创建
         scopedObject = objectFactory.getObject();
         // 设置属性
         simpAttributes.setAttribute(name, scopedObject);
      }
      return scopedObject;
   }
}

remove

  • 删除属性

对点给钱的 SimpAttributes 删除name对应的实体 和 摧毁时调用的回调方法

@Override
@Nullable
public Object remove(String name) {
   SimpAttributes simpAttributes = SimpAttributesContextHolder.currentAttributes();
   synchronized (simpAttributes.getSessionMutex()) {
      Object value = simpAttributes.getAttribute(name);
      if (value != null) {
         simpAttributes.removeAttribute(name);
         return value;
      }
      else {
         return null;
      }
   }
}

registerDestructionCallback

  • 摧毁时的回调方法注册

调用SimpAttributes 进行注册

key: SimpAttributes.class.getName() + ".DESTRUCTION_CALLBACK." + name

value : Runnable

@Override
public void registerDestructionCallback(String name, Runnable callback) {
   SimpAttributesContextHolder.currentAttributes().registerDestructionCallback(name, callback);
}



// org.springframework.messaging.simp.SimpAttributes#registerDestructionCallback
	public void registerDestructionCallback(String name, Runnable callback) {
		synchronized (getSessionMutex()) {
			if (isSessionCompleted()) {
				throw new IllegalStateException("Session id=" + getSessionId() + " already completed");
			}
			this.attributes.put(DESTRUCTION_CALLBACK_NAME_PREFIX + name, callback);
		}
	}

getConversationId

  • SimpAttributesSessionId
@Override
public String getConversationId() {
   return SimpAttributesContextHolder.currentAttributes().getSessionId();
}
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

搜索帮助