users@hk2.java.net

Re: binding a state machine

From: John Wells <john.wells_at_oracle.com>
Date: Thu, 05 Jun 2014 06:21:11 -0400

You can fix these circular injections by doing:

class AbstractState {
  @Inject
  protected javax.inject.Provider<A> a;

@Inject
  protected javax.inject.Provider<B> b;

  @Inject
  protected javax.inject.Provider<C> c;
}

You won't need proxies, but you will now have to call a.get() to get
your instance of A.

On 6/4/2014 10:27 PM, buko wrote:
>
> Was trying to bind a state machine today where every state basically
> has a reference to every other state. Eg:
>
> class AbstractState {
> @Inject
> protected A a;
>
> @Inject
> protected B b;
>
> @Inject
> protected C c;
> }
>
>
> class A extends AbstractState {
> }
>
> class B extends AbstractState {
> }
>
>
> class C extends AbstractState {
> }
>
> ---
> This sent hk2 into an infite loop between trying to create a state and
> resolve its dependencies. Took a while to track down. (Some sort of
> loop detection might be in order.)
>
> The only way to get out of this was to @ProxyForSameScope and
> @UseProxy everywhere. But this is less than desirable since (1)
> proxies have an unknown overhead and (b) considering the above are
> @PerLookup-scope by default, why isn't it possible to resolve all the
> dependencies?
>
> But it seems the code isn't anticipating the possibility of a class
> wanting to inject an instance of itself.
>
> Ideas...?