hk2
  1. hk2
  2. HK2-160

Not clear how to bind JSR 330 provider

    Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Won't Fix
    • Affects Version/s: 2.1.*
    • Fix Version/s: 2.1.*
    • Component/s: None
    • Labels:
      None

      Description

      org.glassfish.hk2.utilities.binding.AbstractBinder has bindFactory() but no bindProvider(). It's not clear what the following Guice code translates into in HK2:

      bind(ScheduledExecutorService.class).toProvider(provider);

      Please add any missing methods, and/or documentation for this use-case.

        Activity

        Hide
        jwells added a comment -

        Just to be clear, JSR-330 Providers are supported for injection. This is how you can do things like lazy initialization or circular dependencies with standard JSR-330 Providers. So

        @Inject Provider<Foo> fooProvider;

        works fine. In fact, we have an enhanced version of Provider:

        https://hk2.java.net/2.2.0-b24/apidocs/org/glassfish/hk2/api/IterableProvider.html

        This is very much like what CDI does with Provider. Guice supports this usage as well, but that is what makes it confusing since it is unclear who is supposed to implement the interface, the user or the container. And the JSR-330 specification does not clear up this confusion at the moment.

        Show
        jwells added a comment - Just to be clear, JSR-330 Providers are supported for injection. This is how you can do things like lazy initialization or circular dependencies with standard JSR-330 Providers. So @Inject Provider<Foo> fooProvider; works fine. In fact, we have an enhanced version of Provider: https://hk2.java.net/2.2.0-b24/apidocs/org/glassfish/hk2/api/IterableProvider.html This is very much like what CDI does with Provider. Guice supports this usage as well, but that is what makes it confusing since it is unclear who is supposed to implement the interface, the user or the container. And the JSR-330 specification does not clear up this confusion at the moment.
        Hide
        jwells added a comment -

        As far as our documentation being a wiki, we are working on that. You might want to check this out (we just did this today, so it is very fresh):

        https://github.com/HK2

        I am going to work on the wiki at some point. You know, when my day job doesn't get in the way!

        Show
        jwells added a comment - As far as our documentation being a wiki, we are working on that. You might want to check this out (we just did this today, so it is very fresh): https://github.com/HK2 I am going to work on the wiki at some point. You know, when my day job doesn't get in the way!
        Hide
        cowwoc added a comment -

        That's big progress in the right direction. Thanks!

        Regarding providers, so what you are saying is that HK2 supports injecting Providers, but not binding them in the first place. Is that correct?

        So you can bind using HK2-specific mechanisms (using services, contracts, factories) and inject them into a Provider. HK2 will then convert those services, contracts and factories into a Provider under the hood. Is that correct?

        Show
        cowwoc added a comment - That's big progress in the right direction. Thanks! Regarding providers, so what you are saying is that HK2 supports injecting Providers, but not binding them in the first place. Is that correct? So you can bind using HK2-specific mechanisms (using services, contracts, factories) and inject them into a Provider. HK2 will then convert those services, contracts and factories into a Provider under the hood. Is that correct?
        Hide
        jwells added a comment -

        Yes. You add "Foo" as a service and if you inject:

        @Inject Provider<Foo> fooProvider

        then in that case we do not create the Foo instance until you do the fooProvider.get()

        This is how you can have lazy services in JSR-330

        Foo can be provided as a normal service, or via a Factory, or via a third-party descriptor and you can still inject it using a Provider.

        Show
        jwells added a comment - Yes. You add "Foo" as a service and if you inject: @Inject Provider<Foo> fooProvider then in that case we do not create the Foo instance until you do the fooProvider.get() This is how you can have lazy services in JSR-330 Foo can be provided as a normal service, or via a Factory, or via a third-party descriptor and you can still inject it using a Provider.
        Hide
        cowwoc added a comment -

        Okay, thanks for the explanation. I'll keep my eyes open for a Wiki (no rush). Github is a major step in the right direction.

        Show
        cowwoc added a comment - Okay, thanks for the explanation. I'll keep my eyes open for a Wiki (no rush). Github is a major step in the right direction.

          People

          • Assignee:
            jwells
            Reporter:
            cowwoc
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: