Custom Scope from User Guide Problem

37 views
Skip to first unread message

Jeff

unread,
Nov 5, 2009, 2:56:17 PM11/5/09
to google-guice
Hi,

I am trying to implement my custom scope so that classes annotated
with my scope:

@MyScoped

can be injected with instances of interface AppRequest into their
constructors.

I have followed all the directions in the User Guide for Custom
Scopes, including copying verbatim the annotation code, the
SimpleScope implementation, and the module code.

Next I triggered the scope and seeded an instance of AppRequest into
the scope object:

scope.seed(Key.get(AppRequest.class), instanceObj);

However, I get an error that AppRequest is not bound when I do the
following:

@MyScoped
class MyClass
{
@Inject
MyClass( AppRequest request )
{ }
}

Obviously I'm missing something, which I think is telling the module
how to inject instances of AppRequest from the scope object. The
users guide does not explain this in the Custom Scope section. Can
someone point me in the right direction?

Thanks,
Jeff

limpb...@gmail.com

unread,
Nov 5, 2009, 8:14:21 PM11/5/09
to google-guice
Do you have a binding for AppRequest? Seeding it isn't sufficient, you
also need a binding. From the SimpleScope.java Javadoc,

* The scope can be initialized with one or more seed values by
calling
* <code>seed(key, value)</code> before the injector will be called
upon to
* provide for this key. A typical use is for a servlet filter to
enter/exit the
* scope, representing a Request Scope, and seed HttpServletRequest
and
* HttpServletResponse. For each key inserted with seed(), it's good
practice
* (since you have to provide <i>some</i> binding anyhow) to include a
* corresponding binding that will throw an exception if Guice is
asked to
* provide for that key if it was not yet seeded: <pre> {@code
*
* bind(key)
* .toProvider(SimpleScope.<KeyClass>seededKeyProvider())
* .in(ScopeAnnotation.class);
* }</pre>

Jeff

unread,
Nov 6, 2009, 11:06:46 AM11/6/09
to google-guice
Thanks Jesse,

That's exactly what I was missing - I don't know how that happened...

Jeff
Reply all
Reply to author
Forward
0 new messages