首页 > 编程语言 > 详细

Spring框架学习--自定义Scope类型

时间:2016-09-14 00:13:33      阅读:411      评论:0      收藏:0      [点我收藏+]

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类型

原文:http://www.cnblogs.com/feixy/p/5870233.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!