users@hk2.java.net

Re: Singleton Scope

From: John Wells <john.wells_at_oracle.com>
Date: Wed, 28 May 2014 14:46:31 -0400

When you are using a binder none of the annotations are honored, instead
all information comes from the binding itself. This allows people to
have ghost annotations and ghost scopes and even different scopes from
what is annotated on the class.

If instead you want annotations to be automatically inspected I would
recommend using addActiveDescriptor
(https://hk2.java.net/2.3.0-b05/apidocs/org/glassfish/hk2/utilities/binding/AbstractBinder.html#addActiveDescriptor%28java.lang.Class%29)
or something like ServiceLocatorUtilities.addClasses
(https://hk2.java.net/2.3.0-b05/apidocs/org/glassfish/hk2/utilities/ServiceLocatorUtilities.html#addClasses%28org.glassfish.hk2.api.ServiceLocator,%20java.lang.Class...%29).

Hope this helps...

On 5/28/2014 1:29 PM, Zack Manning wrote:
> When binding a singleton scoped class within an AbstractBinder, I notice that I always have to chain .in(Singleton.class) to every binding, regardless of if my class has @Singleton at the class level. Does HK2 actually pay attention to the singleton binding on the class? Or is this a bug?
>
> ….
>
> bindAsContract(SingleUseTokenCache.class).in(Singleton.class); //i wish this didnt need the latter
>
> ….
>
> @Singleton
> public class SingleUseTokenCache {
> ….
>
> Just some clarification would be much appreciated as it makes the configuration look muddier than it needs to be.
>
> Zack