users@hk2.java.net

Re: Default scopes for _at_Service annotated services

From: John Wells <john.wells_at_oracle.com>
Date: Sat, 07 Jun 2014 08:21:50 -0400

The rule for defaulting is that services that are NOT annotated with
@Service default to PerLookup (because JSR-330 demands this) but
services marked with @Service default to @Singleton (because we found we
had far greater singleton services than other kinds).

You are correct that hk2 does not currently have the ability to "ghost"
scopes. We do have the ability to ghost qualifiers, but not scopes. I
think this is a bug (though I guess technically it is an improvement).
I have added this Jira to track it:

https://java.net/jira/browse/HK2-213

On 6/7/2014 12:33 AM, Dave Trombley wrote:
>
> I'm trying to dynamically bind some services that will be used
> from (dynamically generated) resources in a jersey application
> container. These services are annotated @Service, but when I try to
> bind them into the appropriate scope
> (org.glassfish.jersey.process.internal.RequestScoped), the reify
> process complains that the default annotations on the class
> (@Singleton, rather than @PerLookup, for some reason?) does not match
> the value passed in the descriptor I tried to bind.
>
> I want to override the default value in the class, somehow. This
> is because I'm locating these things manually from a JAR and loading
> them in, and I do not want to require the developer to create a
> separate version that just adds @RequestScoped to every service. It
> should just be a @Service - my container will "know" how to bind it to
> scope and determine the correct value...
>
> Is this possible to do? How is that default determined? Are
> @Service defaulted @Singleton and that is just the way it is, and
> final? I could write my own @some.other.package.Service, but that's
> real ugly, and still requires code changes...
>
> Thanks again,
> -David