Skip to content

Provide out of the box implementation of the thread scope [SPR-2581] #7270

Closed
@spring-projects-issues

Description

@spring-projects-issues

Eugene Kuleshov opened SPR-2581 and commented

Thread-bound scope can help to address concurrency issues (e.g. batching up requests per thread). So, it would be really handy if Spring provided such scope out of the box for Spring 2.0. E.g. something like this:

public class ThreadScope implements Scope {

  private final ThreadLocal threadScope = new ThreadLocal() {
      protected Object initialValue() {
        return new HashMap();
      }
    };
  
  public Object get(String name, ObjectFactory objectFactory) {
    Map scope = (Map) threadScope.get();
    Object object = scope.get(name);
    if(object==null) {
      object = objectFactory.getObject();
      scope.put(name, object);
    }
    return object;
  }

  public Object remove(String name) {
    Map scope = (Map) threadScope.get();
    return scope.remove(name);
  }

  public void registerDestructionCallback(String name, Runnable callback) {
  }
  
}

Affects: 2.0 final

Referenced from: commits ad2cc34

16 votes, 22 watchers

Metadata

Metadata

Assignees

Labels

has: votes-jiraIssues migrated from JIRA with more than 10 votes at the time of importin: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions