Uploaded image for project: 'CDI Specification Issues'
  1. CDI Specification Issues
  2. CDI-628

Allow overriding of Scope at Injection Point

    XMLWordPrintable

Details

    • Feature Request
    • Resolution: Won't Do
    • Minor
    • None
    • 2.0 .Final
    • None
    • None

    Description

      Allow overriding scopes at injection point.

      @SessionScope
      public class FeatureState {...}
      
      public class Bean B {
        @Inject
        @ConversationScoped //Overriding original scope
        public BeanA beanA;
        ...
      }
      

      Sample use-case for for a feature flag:

      @Alternative
      public class FeatureStateProducer {
      
        @Inject
        @SessionScoped
        private FeatureState devState;
      
        @Inject
        @ConversationScoped
        private FeatureState viewState;
      
        @Inject
        private JNDIProvider jndiProvider;
      
        private boolean viewOnly = true;
      
        @PostConstruct
        public void postConstruct() {
          this.viewMode = ...; //read config.
        }
      
        @Produces
        public FeatureState produce() {
          return viewOnly ? viewState : devState;
        }
      
      }
      

      Currently this can be solved by overriding scope with @Qualifiers:

      @ViewMode
      @ConversationScoped
      public class ViewFeatureState extends FeatureState {...}
      
      @DevMode
      @SessionScoped
      public class DevFeatureState extends FeatureState {...}
      

      Risks:

      • developers might start to define the scopes at IP and on bean (hard to maintain).
      • confusing when debugging, annotation on bean, e.g. @ApplicationScoped yet behaving differently.
      • etc...

      Attachments

        Activity

          People

            Unassigned Unassigned
            sknitelius Stephan Knitelius (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: