Skip to content

Instantly share code, notes, and snippets.

@radutoev
Last active March 14, 2018 20:12
Show Gist options
  • Save radutoev/3b79c2083ab6d99750539ead4108519f to your computer and use it in GitHub Desktop.
Save radutoev/3b79c2083ab6d99750539ead4108519f to your computer and use it in GitHub Desktop.
public class InheritedThreadScope implements Scope {
private static final Logger logger = LoggerFactory.getLogger(InheritedThreadScope.class);
private final ThreadLocal<Map<String, Object>> threadScope =
new InheritableThreadLocal() {
@Override
protected Map<String, Object> initialValue() {
return new HashMap<>();
}
};
public Object get(String name, ObjectFactory<?> objectFactory) {
Map<String, Object> scope = this.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<String, Object> scope = this.threadScope.get();
return scope.remove(name);
}
public void registerDestructionCallback(String name, Runnable callback) {
logger.warn("InheritedThreadScope does not support destruction callbacks. " +
"Consider using RequestScope in a web environment.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment