Custom Request Scope

198 views
Skip to first unread message

rod

unread,
Aug 6, 2011, 7:32:35 PM8/6/11
to google...@googlegroups.com
My application implements its own web server and I'd like to have a
scope for the request/response/etc... objects that get created on each
request and injected into all the individual request handling classes.
at the moment these are being manually created and injected, but i'd
like to be able to do this with guice. i've been reading the docs but
need a little advice - can i accomplish this with any of the built in
guice scopes, or will i need to create a custom scope (docs say this
is not reccomended!).

thanks,
rod.

Christopher Currie

unread,
Aug 6, 2011, 9:27:47 PM8/6/11
to google...@googlegroups.com
Have you looked at the Guice Servlet [1] extension? Among other
features, it defines a @Request scope that is designed for exactly
what you describe.

[1] http://code.google.com/p/google-guice/wiki/Servlets

> --
> You received this message because you are subscribed to the Google Groups
> "google-guice" group.
> To post to this group, send email to google...@googlegroups.com.
> To unsubscribe from this group, send email to
> google-guice...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/google-guice?hl=en.
>
>

rod

unread,
Aug 7, 2011, 5:38:59 AM8/7/11
to google...@googlegroups.com
Hi. Yes I did read these docs, but my app implements it's own simple web server and does not use servlets or a servlet container, which is why I was confused about if I'd be able to use this. Re reading the docs it still doesn't seem possible to use the servlet request scope in my application, and it looks like I will need to write a custom scope. Can anyone confirm this please?

Thanks a lot,
Rod.

Witold Szczerba

unread,
Aug 7, 2011, 6:23:21 AM8/7/11
to google...@googlegroups.com
Last year, I have implemented my own scope for Adobe/Day CQ based
application framework which introduces dependency injection everywhere
plus one JSP TAG which lookups objects in injector.
What I was is almost identical to RequestScope of the aforementioned
Guice Servlet extension with slightly different integration. In main
module I bind that scope implementation with my own @RequestScoped
annotation. In your case - I suggest you to do the same. Read the
Guice Servlet source code, especially the scope implementations and
create your own based on that - it will help you understand how does
scope work behind the scenes.

Regards,
Witold Szczerba

Fred Faber

unread,
Aug 7, 2011, 9:48:28 AM8/7/11
to google...@googlegroups.com
You can use the implementation of RequestScope outside of a servlet container.

Look here:

You can call this method by wrapping your request thread's entry point in a Callable.

For instance if your logic now is:

public void handlRequest(...) {
  ...handles the request
}

You'd do:

public void handleRequest(...) {
  ServletScopes.scopeRequest(new Callable<Void>() {
     @Override public Void call() throws Exception {
         scopedHandleRequest9);
      }
    }, Collections.emptyMap()).call();
}  

/** This is the original handleRequest, but renamed... */
private void scopedHandleRequest(...) {
  ....
}

Fred

rod

unread,
Aug 10, 2011, 11:09:48 AM8/10/11
to google...@googlegroups.com
Ok, thanks a lot for the advice guys!

rod.

Reply all
Reply to author
Forward
0 new messages