Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
public class ThreadScope implements Scope {
static final Object NULL = new Object();
@Override
public <T> Provider<T> scope(Key<T> key, final Provider<T> unscoped) {
return new Provider<T>() {
final ThreadLocal<T> storage = new ThreadLocal<T>();
@Override
@SuppressWarnings("unchecked")
public T get() {
T obj = storage.get();
if (obj == null) {
obj = unscoped.get();
storage.set(obj != null ? obj : (T) NULL);
}
return obj == NULL ? null : obj;
}
};
}
}
public class ThreadScopeBindingModule extends AbstractModule {
@Override
protected void configure() {
ThreadScope scope = new ThreadScope();
bindScope(ThreadScoped.class, scope);
}
}
bind(SomeInterface.class).toProvider(SomeImplementationProvider.class).in(ThreadScoped.class);
Создание Custom Scope в JEE и Spring