:Guiceの:Requestスコープに注釈付きの型を注入することはできません、私がリクエストスコープに注釈付きの変数を注入しようとしています
/**
* A Callable that is constructed in one scope and injects a Callable into a potentially separate
* scope.
* <p/>
* @param <V> the type of object returned by the Callable
* @author Gili Tzabari
*/
public final class InjectingCallable<V> implements Callable<V>
{
private final Injector injector;
private final Class<? extends Callable<V>> delegate;
/**
* Creates a new InjectingCallable.
* <p/>
* @param injector the Guice injector
* @param delegate the class to inject and delegate to
*/
public InjectingCallable(Injector injector, Class<? extends Callable<V>> delegate)
{
Preconditions.checkNotNull(injector, "injector may not be null");
Preconditions.checkNotNull(delegate, "delegate may not be null");
this.injector = injector;
this.delegate = delegate;
}
@Override
public V call() throws Exception
{
return injector.getInstance(delegate).call();
}
}
GetModule:InjectingCallableはRequestスコープ内GetModuleを注入し、
Map<Key<?>, Object> seedMap = ImmutableMap.<Key<?>, Object>builder().
put(Key.get(String.class, Names.named("name")), name).build();
return ServletScopes.scopeRequest(new InjectingCallable<>(injector,
GetModule.class), seedMap).call();
@RequestScoped
private static class GetModule implements Callable<Module>
{
private final String name;
private final Session session;
@Inject
public GetModule(@Named("name") String name, Session session)
{
this.name = name;
this.session = session;
}
}
このコードを実行すると、このエラーが発生しますROR:私はそれが動作グローバルスコープに同じ変数をバインドする場合
1) No implementation for java.lang.String annotated with @com.google.inject.name.Named(value=name) was bound.
while locating java.lang.String annotated with @com.google.inject.name.Named(value=name)
。注釈を削除すると、その注釈が機能します。この問題は、Requestスコープの注釈付き変数に固有のようです。何か案は?
ImmutableMapの場合は、左辺にジェネリックスがあるので、イコール記号の右側の 'builder()'コールでジェネリックを指定する必要はありません。それはstatic builder()メソッドの全体的なポイントです。すなわち、 - ImmutableMap、オブジェクト> seedMap = ImmutableMap.builder()。put(...)。build() ' –
Tom
@Tom、私はそれを行うと私はコンパイラのエラーを取得します。Java7 update 2では、デフォルトで