users@hk2.java.net

Re: Custom injection resolver non Singleton

From: john wells <john.wells_at_oracle.com>
Date: Thu, 21 Apr 2016 20:34:24 -0400

You may be confusing the HttpRequest implemention in the example with
the one from Jersey. The one from Jersey is in a proxiable scope. The
thing actually injected is a proxy, basically a shell around the true
implementation, which is changed behind the scenes whenever the request
is modified.

As far as documentation over code, I'd have to look at the example again
and make sure it is still correct, sometimes things change and we don't
always get all the places in the documentation that cover that specific
behavior.

Hope that helps!

On 4/21/2016 2:07 AM, Trenton D. Adams wrote:
> I'm further confused by this. The custom-resolver example in the
> source does this...
> ​ ​
> @Inject
> private HttpRequest request;
>
> The problem with that, is that it's a singleton, and you could have
> synchronization issues, thereby share user data in different requests.
>
> I've taken a quick look at the HK2 source, and I do not see any
> synchronization over injecting into a resolver and then calling it,
> which is why there's no guarantee that "request" is in fact the one
> related to the current HttpRequest.
>
> Am I missing something in the code? I know the code is quite complex,
> so I might be.
>
>
> On Wed, Apr 20, 2016 at 5:30 PM, Trenton D. Adams
> <trenton.d.adams_at_gmail.com <mailto:trenton.d.adams_at_gmail.com>> wrote:
>
> According to the documentation, custom injection resolvers do not
> need to be of the Singleton scope. But there's code in HK2 that
> forces them to be in that scope. Is this a design problem, or a
> documentation problem?
>
> https://hk2.java.net/custom-resolver-example.html
>
> It says...
> The AlternateInjectResolver is in the @Singleton context, which is
> the usual context for implementations of InjectionResolver
> <https://hk2.java.net/apidocs/org/glassfish/hk2/api/InjectionResolver.html>.
> In general however implementations of InjectionResolver
> <https://hk2.java.net/apidocs/org/glassfish/hk2/api/InjectionResolver.html> may
> be in any context.
>
>