首页 > 代码库 > Spring框架学习--自定义Scope类型
Spring框架学习--自定义Scope类型
1.首先需要定义一个Scope借口的实现类,至少需要实现get和remove方法:
/** * Created by feiyu on 16/9/13. */public class ThreadScope implements Scope { private final ThreadLocal<Map<String, Object>> threadScope = new ThreadLocal<Map<String, Object>>(){ @Override protected Map<String, Object> initialValue() { return new HashMap<String,Object>(); } }; public Object get(String name, ObjectFactory<?> objectFactory) { Map<String,Object> scope = threadScope.get(); Object obj = scope.get(name); if(obj == null){ obj = objectFactory.getObject(); scope.put(name, obj); } return obj; } public Object remove(String name) { Map<String,Object> scope = threadScope.get(); return scope.remove(name); } public void registerDestructionCallback(String name, Runnable callback) { } public Object resolveContextualObject(String key) { return null; } public String getConversationId() { return null; }
2.注册Scope
Scope threadScope = new ThreadScope();beanFactory.registerScope("thread", threadScope);
3.使用Scope
<bean id=".." class=".." scope="thread"/>
Spring框架学习--自定义Scope类型
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。