Detecting the scope associated with a binding?

201 views
Skip to first unread message

Gili

unread,
Oct 6, 2008, 2:24:19 PM10/6/08
to google-guice
Hi,

Is it possible to detect the scope associated with Binding returned by
Injector.getBinding(Key)? I'm trying to detect at runtime whether a
user specified multiple Scopes from different frameworks, such as
Guice, Spring, and Jersey (JAX-RS) so I can throw an error.

PS: What happens when a user associates two scope annotations with the
same class? I assume it throws an error?

Thanks,
Gili

Stuart McCulloch

unread,
Oct 6, 2008, 2:42:51 PM10/6/08
to google...@googlegroups.com
2008/10/7 Gili <gili.t...@gmail.com>

Hi,

Is it possible to detect the scope associated with Binding returned by
Injector.getBinding(Key)? I'm trying to detect at runtime whether a
user specified multiple Scopes from different frameworks, such as
Guice, Spring, and Jersey (JAX-RS) so I can throw an error.

well in the current trunk Binding has a visitor method for scopes:

  /**
   * Accepts a scoping visitor. Invokes the visitor method specific to this binding's scoping.
   *
   * @param visitor to call back on
   */
  <V> V acceptScopingVisitor(BindingScopingVisitor<V> visitor);

which should do what you want... the new SPI looks very cool btw!
 
PS: What happens when a user associates two scope annotations with the
same class? I assume it throws an error?

yes, you should see an error (see ScopeBindingProcessor.java)
 
Thanks,
Gili




--
Cheers, Stuart

Gili Tzabari

unread,
Oct 6, 2008, 4:12:49 PM10/6/08
to google...@googlegroups.com

Which new SPI? :)

Gili

Stuart McCulloch wrote:
> 2008/10/7 Gili <gili.t...@gmail.com <mailto:gili.t...@gmail.com>>

Stuart McCulloch

unread,
Oct 6, 2008, 10:25:07 PM10/6/08
to google...@googlegroups.com
2008/10/7 Gili Tzabari <gili.t...@gmail.com>

   Which new SPI? :)

the one Jesse mentioned here... :)

   http://groups.google.com/group/google-guice/browse_thread/thread/5ccc1e22d53d144b

which will be a major part of the next release.
 



--
Cheers, Stuart

Gili

unread,
Nov 21, 2008, 3:04:16 PM11/21/08
to google-guice
Jesse,

Can you please explain the difference between
BindingScopingVisitor.visitEagerSingleton() and visitScope(Scope
scope)? I assume there is a reason you didn't just invoke visitScope
(Scope.Singleton) instead of exposing a separate method?

Thanks,
Gili

On Oct 6, 9:25 pm, "Stuart McCulloch" <mccu...@gmail.com> wrote:
> 2008/10/7 Gili Tzabari <gili.tzab...@gmail.com>
>
>
>
> >    Which new SPI? :)
>
> the one Jesse mentioned here... :)
>
> http://groups.google.com/group/google-guice/browse_thread/thread/5ccc...
>
> which will be a major part of the next release.
>
>
>
> > Gili
>
> > Stuart McCulloch wrote:
> > > 2008/10/7 Gili <gili.tzab...@gmail.com <mailto:gili.tzab...@gmail.com>>

limpb...@gmail.com

unread,
Nov 21, 2008, 7:17:12 PM11/21/08
to google-guice
On Nov 21, 12:04 pm, Gili <gili.tzab...@gmail.com> wrote:
> Jesse,
>
> Can you please explain the difference between
> BindingScopingVisitor.visitEagerSingleton() and visitScope(Scope
> scope)? I assume there is a reason you didn't just invoke visitScope
> (Scope.Singleton) instead of exposing a separate method?

Eagerness. I've updated the Scopes wiki with a full rundown:
http://code.google.com/p/google-guice/wiki/Scopes

Gili

unread,
Nov 21, 2008, 8:07:55 PM11/21/08
to google-guice
Thanks! More questions...

What is "Injector binding"? BindingScopingVisitor refers to it but I
tried search for the term and didn't find a definition anywhere. It
might be helpful to define this in the Javadoc brief description on
top. In fact, maybe you should define this in http://code.google.com/p/google-guice/wiki/Scopes
and link to it from the Javadoc on top -- I found this wiki page to be
a very good read!

Also, visitNoScoping's documentation reads "On a module, this strategy
indicates that the injector should use scoping annotations to find a
scope." Can you please give example of what this means? Also, what
does "An unscoped binding will behave like a scoped one when it is
linked to a scoped binding" mean? It's not clear to me how scopes can
be "linked".

Finally, there is still a lot of confusion in my mind on the topic of
"scope annotations". visitScopeAnnotation() and Binder.bindScope()
make me believe that you could bind a Scope to an arbitrary annotation
(such as @Red) such that:

@Red
String username;

Binder.bind(String.class).annotatedWith(Red.class).to(Foo.class);
Binder.bindScope(Red.class, Singleton.class);

would mean that username is bound to Foo.class but also has a
Singleton scope. On the other hand, http://code.google.com/p/google-guice/wiki/Scopes
makes me believe that "scope annotation" refers to Singleton.class --
that is, an annotation *for* a Scope. Please clarify the Wiki page and/
or BindingScopingVisitor to explicitly define what a scope annotation
is :)

Thank you,
Gili

Gili

unread,
Nov 22, 2008, 12:14:06 PM11/22/08
to google-guice
I now understand that Binder.bindScope() is used to bind an annotation
such as Singleton.class to a Scope instance such as Scopes.SINGLETON,
but it isn't clear to me how one can look up this binding after the
fact. That is, when BindingScopingVisitor.visitScopeAnnotation() is
invoked, how do I find out what scope is associated with that
annotation?

I still haven't figured out the other outstanding questions regarding
Injector Binding and visitNoScoping's "linked scope".

Gili

On Nov 21, 8:07 pm, Gili <gili.tzab...@gmail.com> wrote:
> Thanks! More questions...
>
> What is "Injector binding"? BindingScopingVisitor refers to it but I
> tried search for the term and didn't find a definition anywhere. It
> might be helpful to define this in the Javadoc brief description on
> top. In fact, maybe you should define this inhttp://code.google.com/p/google-guice/wiki/Scopes
> and link to it from the Javadoc on top -- I found this wiki page to be
> a very good read!
>
> Also, visitNoScoping's documentation reads "On a module, this strategy
> indicates that the injector should use scoping annotations to find a
> scope." Can you please give example of what this means? Also, what
> does "An unscoped binding will behave like a scoped one when it is
> linked to a scoped binding" mean? It's not clear to me how scopes can
> be "linked".
>
> Finally, there is still a lot of confusion in my mind on the topic of
> "scope annotations". visitScopeAnnotation() and Binder.bindScope()
> make me believe that you could bind a Scope to an arbitrary annotation
> (such as @Red) such that:
>
> @Red
> String username;
>
> Binder.bind(String.class).annotatedWith(Red.class).to(Foo.class);
> Binder.bindScope(Red.class, Singleton.class);
>
> would mean that username is bound to Foo.class but also has a
> Singleton scope. On the other hand,http://code.google.com/p/google-guice/wiki/Scopes

limpb...@gmail.com

unread,
Nov 22, 2008, 5:32:41 PM11/22/08
to google-guice
On Nov 22, 9:14 am, Gili <gili.tzab...@gmail.com> wrote:
> I now understand that Binder.bindScope() is used to bind an annotation
> such as Singleton.class to a Scope instance such as Scopes.SINGLETON,
> but it isn't clear to me how one can look up this binding after the
> fact. That is, when BindingScopingVisitor.visitScopeAnnotation() is
> invoked, how do I find out what scope is associated with that
> annotation?

You'll need to scan through the Elements in search of a ScopeBinding.
http://google-guice.googlecode.com/svn/trunk/latest-javadoc/com/google/inject/spi/ScopeBinding.html

If your modules are consistent, you'll have exactly one ScopeBinding
for each scoping annotation.

Gili

unread,
Nov 22, 2008, 6:28:33 PM11/22/08
to google-guice
Thanks Jesse! What about the other questions? :)

PS: I am very close to published an updated Jersey-Guice integration
module that is on-par with Jersey-Spring.

Gili

On Nov 22, 5:32 pm, "je...@swank.ca" <limpbiz...@gmail.com> wrote:
> On Nov 22, 9:14 am, Gili <gili.tzab...@gmail.com> wrote:
>
> > I now understand that Binder.bindScope() is used to bind an annotation
> > such as Singleton.class to a Scope instance such as Scopes.SINGLETON,
> > but it isn't clear to me how one can look up this binding after the
> > fact. That is, when BindingScopingVisitor.visitScopeAnnotation() is
> > invoked, how do I find out what scope is associated with that
> > annotation?
>
> You'll need to scan through the Elements in search of a ScopeBinding.http://google-guice.googlecode.com/svn/trunk/latest-javadoc/com/googl...

limpb...@gmail.com

unread,
Nov 22, 2008, 8:08:45 PM11/22/08
to google-guice
On Nov 21, 5:07 pm, Gili <gili.tzab...@gmail.com> wrote:
> What is "Injector binding"?
That's defined here:
http://google-guice.googlecode.com/svn/trunk/latest-javadoc/com/google/inject/Binding.html

> Also, visitNoScoping's documentation reads "On a module, this strategy
> indicates that the injector should use scoping annotations to find a
> scope." Can you please give example of what this means? Also, what
> does "An unscoped binding will behave like a scoped one when it is
> linked to a scoped binding" mean? It's not clear to me how scopes can
> be "linked".
Suppose you bind Foo to FooImpl. If Foo is unscoped and FooImpl is a
singleton, then Foo is effectively a singleton.

> Finally, there is still a lot of confusion in my mind on the topic
of
> "scope annotations".

I took another pass on the scopes wiki. Hopefully it makes it more
obvious what you'll need to do to define a custom scope:
http://code.google.com/p/google-guice/wiki/Scopes

Gili Tzabari

unread,
Nov 22, 2008, 8:40:35 PM11/22/08
to google...@googlegroups.com
je...@swank.ca wrote:
> On Nov 21, 5:07 pm, Gili <gili.tzab...@gmail.com> wrote:
>> What is "Injector binding"?
> That's defined here:
> http://google-guice.googlecode.com/svn/trunk/latest-javadoc/com/google/inject/Binding.html

I assume that Injector.getBinding(Key).acceptScopingVisitor() is the
way to use BindingScopingVisitor on injector bindings, but it isn't
clear how to use it against module bindings.

I would like to suggest the following:

- visitEagerSingleton() should link to
ScopedBindingBuilder.asEagerSingleton()

- visitNoScoping() "An unscoped binding will behave like a scoped one
when it is linked to a scoped binding." should probably reads "[...]
linked to a scoped *target*". I think "target" is easier to understand
in this context (too many uses of the word binding). Alternatively, your
other explanation is also much clearer: "Suppose you bind Foo to

FooImpl. If Foo is unscoped and FooImpl is a singleton, then Foo is
effectively a singleton."

- visitScopeAnnotation(): It isn't clear when this method is even
invoked. I tried invoking Binder.bindScope() and in(scopedAnnotation)
and neither of these triggered this method. visitScope() was triggered
instead. Remember, I am using the Guice-2.0 snapshot, not trunk.

> I took another pass on the scopes wiki. Hopefully it makes it more
> obvious what you'll need to do to define a custom scope:
> http://code.google.com/p/google-guice/wiki/Scopes

Thanks!

Gili

Gili

unread,
Nov 24, 2008, 10:45:30 AM11/24/08
to google-guice
Jesse,

It isn't clear how to get at Module bindings.

BindingScopingVisitor.visitScopeAnnotation() says "This scope strategy
is found only on module bindings", but it isn't clear to me how to
fire a BindingScopingVisitor against a Module in the first place. I
can see Injector.getBinding(Class) returning a Binding and then a
Binding.acceptScopingVisitor() taking BindingScopingVisitor but I
don't see the same path for a Module. That is my main source of
confusion with respect to "Injector bindings" versus "Module bindings"
-- as far as I can tell there is only Injector bindings.

Gili

On Nov 22, 8:40 pm, Gili Tzabari <gili.tzab...@gmail.com> wrote:
> je...@swank.ca wrote:
> > On Nov 21, 5:07 pm, Gili <gili.tzab...@gmail.com> wrote:
> >> What is "Injector binding"?
> > That's defined here:
> >http://google-guice.googlecode.com/svn/trunk/latest-javadoc/com/googl...

limpb...@gmail.com

unread,
Nov 24, 2008, 1:52:13 PM11/24/08
to google-guice

Gili Tzabari

unread,
Nov 24, 2008, 2:14:00 PM11/24/08
to google...@googlegroups.com

Ah! I missed that. This looks a little contrived:

Elements.getElements(Module).acceptVisitor(new DefaultElementVisitor<Void>()
{
Void visitBinding(Binding<Void> binding)
{
binding.acceptScopingVisitor(BindingScopingVisitor)
}
});

Wouldn't one simply use ElementVisitor.visitScopeBinding() for Module
Bindings instead? It's simpler and contains more information. If so, you
might want to consider removing
BindingScopingVisitor.visitScopeAnnotation() altogether.

Thanks for the head's up regarding Elements!

Gili

limpb...@gmail.com

unread,
Nov 24, 2008, 4:15:59 PM11/24/08
to google-guice
On Nov 24, 11:14 am, Gili Tzabari <gili.tzab...@gmail.com> wrote:
> Wouldn't one simply use ElementVisitor.visitScopeBinding() for Module
> Bindings instead? It's simpler and contains more information. If so, you
> might want to consider removing
> BindingScopingVisitor.visitScopeAnnotation() altogether.

They serve different purposes.

The scope of this binding triggers
BindingScopingVisitor.visitScopeAnnotation():
bind(Foo.class).to(RealFoo.class).in(BatchScoped.class);
This tells Guice that the binding for Foo should be scoped using
@BatchScope.

This bind scope statement triggers ElementVisitor.visitScopeBinding():
bindScope(BatchScoped.class, new MyBatchScope());
This tells Guice that @BatchScope is implemented using a particular
implementation.
Reply all
Reply to author
Forward
0 new messages