Last active
March 14, 2018 20:12
-
-
Save radutoev/3b79c2083ab6d99750539ead4108519f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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