Using RequestScope

1,415 views
Skip to first unread message

atoy40

unread,
Jun 16, 2011, 10:59:44 AM6/16/11
to google-guice
Hello,

i'm trying to follow the example at http://code.google.com/p/google-guice/wiki/ServletModule
named "Using RequestScope".
I need to inject a username (a string) classes of my webapps.

So a added a servlet filter doing something like :
---8<---
httpRequest.setAttribute(Key.get(String.class,
Names.named("username")).toString(), my_username));
---8<---
and i added the filter mapping in my guice ServletModule

Then in a class requesting the username, an injected it as :
---8<---
private final Provider<String> username;

@Inject
public MyUserService(@Named("username") Provider<String> username) {
this.username = username
}
---8<---

but when the MyUserService class is instancied by guice, i get an
exception :
No implementation for java.lang.String annotated with
@com.google.inject.name.Named(value=username) was bound.
while locating com.google.inject.Provider<java.lang.String>
annotated with @com.google.inject.name.Named(value=username)

Any idea on how to inject the "Key" added to servlet attribute ?

Thanks
Anthony.

atoy40

unread,
Jun 16, 2011, 12:11:05 PM6/16/11
to google-guice
Mmmhhhh .... while waiting to be moderated, i tested some stuffs.
One is working !
I added this binding to my guice module :

bind(Key.get(String.class,
Names.named("username"))).to(String.class).in(RequestScoped.class);

but i don't know if it's the good way to do it ?

Anthony.

On Jun 16, 4:59 pm, atoy40 <anthony.hinsin...@gmail.com> wrote:
> Hello,
>
> i'm trying to follow the example athttp://code.google.com/p/google-guice/wiki/ServletModule
Reply all
Reply to author
Forward
0 new messages