码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-09-14 00:13:46      阅读:216      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!