@Named variables cannot be scoped?

14 views
Skip to first unread message

Gili Tzabari

unread,
Jan 23, 2012, 10:32:03 AM1/23/12
to google-guice
Hi,

I'm having problems injecting @Named variables inside the request
scope. Removing @Named or attempting to resolve those same variables
in the global scope works fine.

When I attempt to inject the following class:

@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)
[...]

using:

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();

Where "InjectingCallable.call()" invokes
"Injector.getInstance(GetModule.class).call()" inside the REQUEST
scope.

I get this error:

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)

However, if I drop the @Named annotation everything works. I tracked
this down to InjectorImpl:831 that throws an error if an annotated
binding cannot be resolved in the UNSCOPED scope. Why does it throw
this exception? Shouldn't be able to resolve @Named variables in the
request scope? Is this a bug?

Thanks,
Gili
Reply all
Reply to author
Forward
0 new messages