1 Star 0 Fork 156

whl595145999 / Spring-Analysis

forked from huifer / Code-Analysis 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Spring-SimpleMapScope.md 2.21 KB
一键复制 编辑 原始数据 按行查看 历史
huifer 提交于 2020-11-06 15:51 . :bookmark:spring scope 接口分析

Spring SimpleMapScope

  • 类全路径: org.springframework.context.testfixture.SimpleMapScope

内部变量

/**
 * 存储对象容器
 * key: name
 * value: object instance
 */
private final Map<String, Object> map = new HashMap<>();

/**
 * 存储回调方法
 */
private final List<Runnable> callbacks = new LinkedList<>();

SimpleMapScope主要围绕上述两个内部变量进行操作.

方法分析

get

  • 获取实例

逻辑

  1. 从容器中获取
    1. 不存在
      1. 从 ObjectFactory 中获取
        1. 放入容器
	@Override
	public Object get(String name, ObjectFactory<?> objectFactory) {
		synchronized (this.map) {
			// 从 容器中获取 .
			Object scopedObject = this.map.get(name);
			// 不存在从 ObjectFactory 中获取
			if (scopedObject == null) {
				scopedObject = objectFactory.getObject();
				// 添加到容器
				this.map.put(name, scopedObject);
			}
			return scopedObject;
		}
	}

remove

  • 删除 name 对应的Object 实例
@Override
public Object remove(String name) {
   synchronized (this.map) {
      return this.map.remove(name);
   }
}

registerDestructionCallback

  • 添加摧毁方法的回调
@Override
public void registerDestructionCallback(String name, Runnable callback) {
   this.callbacks.add(callback);
}

close

  • 在 close 的时候调用所有注册的回调方法
public void close() {
   for (Iterator<Runnable> it = this.callbacks.iterator(); it.hasNext();) {
      Runnable runnable = it.next();
      runnable.run();
   }
}
Java
1
https://gitee.com/595145999/spring-analysis.git
git@gitee.com:595145999/spring-analysis.git
595145999
spring-analysis
Spring-Analysis
master

搜索帮助