标签:
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"/>
标签:
原文地址:http://www.cnblogs.com/feixy/p/5870233.html