users@hk2.java.net

Re: _at_Inject specific instance

From: john wells <john.wells_at_oracle.com>
Date: Wed, 20 Apr 2016 22:20:44 -0400

Yes that sounds like the right track. However, there *are* ways to
inject a specific instance (if you have created the object yourself).
API like this allow for it:

https://hk2.java.net/2.5.0-b04/apidocs/org/glassfish/hk2/utilities/ServiceLocatorUtilities.html#addOneConstant%28org.glassfish.hk2.api.ServiceLocator,%20java.lang.Object,%20java.lang.String,%20java.lang.reflect.Type...%29

But it is better to let hk2 and/or CDI to do the injection/creation itself.

On 4/20/2016 6:55 PM, Trenton D. Adams wrote:
> I get it now, there is no way of doing this. It's called Context and
> Dependency Injection for a reason, because the framework using CDI
> should have the context of each object instance, in which case it will
> know how to provide injections from a factory, based on the context.
>
> @Context HttpServletRequest request in the case of JAX-RS simply means
> inject the current servlet request, the framework knows the context,
> so it's able to do that.
>
> Am I on the right track here?
>
> On Tue, Apr 19, 2016 at 7:12 PM, Trenton D. Adams
> <trenton.d.adams_at_gmail.com <mailto:trenton.d.adams_at_gmail.com>> wrote:
>
> So I've created some basic HK2 services and what not, and HK2 is
> able to create instances for me. What I don't see any of the docs
> mentioning is how to get a specific instance of a Class injected
> into another class.
>
> When I specify...
> @Inject private SpecialClassInterface specialClass;
> SpecialClass gets injected as a new empty instance. But what I
> want is it to be injected with the instance I wanted it injected
> with. I don't see any docs on that.
>
> I'm guessing it might be a more fundamental lack of understanding
> of HK2 as a whole, but I don't know where to start. I've read
> quite a bit of docs, and nothing mentions this.
>
>