users@jersey.java.net

[Jersey] Re: [Jersey 2] Integrate HK2 with Picocontainer - lazy resolution of singletons

From: Marek Potociar <marek.potociar_at_oracle.com>
Date: Wed, 20 Feb 2013 14:54:38 +0100

On Feb 19, 2013, at 6:38 PM, marianl <marianl_at_o2.pl> wrote:

> Hello,
>
> I have DI container and I'd like to expose its singletons for Jersey.
> I've seen that there are AbstractBuilder and Injections which might help with that.
>
> Getting instances from picocontainer and binding them inside AbstractBuilder#configure method works, but I don't want to do eager binding. I'd like to create instances later, when they are needed.
>
> How can I achieve that?

Use HK2 org.glassfish.hk2.api.Factory concept. That's essentially a "source" Provider as it is known in JSR 330 (Java DI).

And then in your Binder implementation you would use:

bindFactory(MyFactory.class).to(ContractA.class)...

...or similar construct.

HTH,
Marek
>
> Issues I've encountered:
> I don't how can I use class bindings because when I tried that then HK2 says it cannot resolve dependencies. Is there some contract I need to implement to get a chance of providing concrete instance of desired type?
> I don't know if I'm missing something or there is other way to do it.
>
> Possible solution:
> Bind java.lang.reflect.Proxy which will do lazy resolution.
>
> Kind regards,
> Marek