hk2
  1. hk2
  2. HK2-196

Ability to get Injectee/ServiceHandle within a Factory Implementation

    Details

    • Type: New Feature New Feature
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.2.0
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None

      Description

      InjectionResolver burden developers with having to create custom injection annotation and resolver implementation. It would be nice if one could simply piggyback on the ThreeThirtyResolver and create a factory class that performs injection resolution. Perhaps something like this:

      @Service
      public class LoggerFactoryResolver implements Factory<Logger> {
      
        private final Injectee injectee; //proxyed?
        private final ServiceHandle<?> handler; //proxyed?
        
        @Inject
        LoggerFactoryResolver(Injectee injectee, ServiceHandle handler) {
          this.injectee = injectee;
          this.handler = handler;
        }
      
        @Override
        public Logger provide() {
          AnnotatedElement parent = injectee.getParent();
          // do some magic
          
          return ...;
        }
        
        @Override
        public void dispose(T instance) {
        }
      
      }
      
      

        Activity

        Hide
        jwells added a comment -

        You know you don't need to create a new annotation in order to do something like this. All you need to do is implement the InjectionResolver, give it higher priority than the default one, and inject the named ThreeThirtyResolver into that one. Kind of like this:

        @Service @Rank(100)
        public class AssistedInjectResolver implements InjectionResolver<Inject>

        { @Inject @Named(InjectionResolver.SYSTEM_RESOLVER_NAME) private InjectionResolver<Inject> systemResolver; ... }

        And now you are helping with the resolution of @Inject and using the default ThreeThirtyResolver in order to do all the normal stuff it would normally do.

        If this solves your problem then maybe the thing that should be added is a direct example of someone overriding @Inject to do assisted injection in this manner.

        Show
        jwells added a comment - You know you don't need to create a new annotation in order to do something like this. All you need to do is implement the InjectionResolver, give it higher priority than the default one, and inject the named ThreeThirtyResolver into that one. Kind of like this: @Service @Rank(100) public class AssistedInjectResolver implements InjectionResolver<Inject> { @Inject @Named(InjectionResolver.SYSTEM_RESOLVER_NAME) private InjectionResolver<Inject> systemResolver; ... } And now you are helping with the resolution of @Inject and using the default ThreeThirtyResolver in order to do all the normal stuff it would normally do. If this solves your problem then maybe the thing that should be added is a direct example of someone overriding @Inject to do assisted injection in this manner.
        Hide
        saden added a comment -

        I didn't know that! This definitely solves my issue. An example in documentation would help.

        Thanks.

        Show
        saden added a comment - I didn't know that! This definitely solves my issue. An example in documentation would help. Thanks.
        Hide
        jwells added a comment -

        OK, I'll keep this bug open until an example of assisted inject with @Inject is created.

        Show
        jwells added a comment - OK, I'll keep this bug open until an example of assisted inject with @Inject is created.
        Hide
        jwells added a comment -

        We added https://hk2.java.net/2.4.0-b25/apidocs/org/glassfish/hk2/api/InstantiationService.html which is a service that can be injected into Factory implementations that will contain information about the Injectee for which the factory provide method is being called.

        This is also reflected in the Factory javadoc:
        https://hk2.java.net/2.4.0-b25/apidocs/org/glassfish/hk2/api/Factory.html

        Show
        jwells added a comment - We added https://hk2.java.net/2.4.0-b25/apidocs/org/glassfish/hk2/api/InstantiationService.html which is a service that can be injected into Factory implementations that will contain information about the Injectee for which the factory provide method is being called. This is also reflected in the Factory javadoc: https://hk2.java.net/2.4.0-b25/apidocs/org/glassfish/hk2/api/Factory.html

          People

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

            Dates

            • Created:
              Updated:
              Resolved: