Uploaded image for project: 'Magnolia'
  1. Magnolia
  2. MAGNOLIA-4471

Support for specifying multiple types on a component

XMLWordPrintable

    • Icon: Improvement Improvement
    • Resolution: Won't Do
    • Icon: Neutral Neutral
    • None
    • 4.5.3
    • core
    • None

      When specifying a component in a module descriptor you give its type and its implementation (among other things). The key is the type you will be using when declaring a parameter to be injected in the constructor of another class.

      Such as:

          <component>
            <type>info.magnolia.cms.i18n.MessagesManager</type>
            <implementation>info.magnolia.cms.i18n.DefaultMessagesManager</implementation>
          </component>
      

      This works fine, except when there's a hierarchy of component providers. This test case illustrates the problem:

      public class GuiceMultipleKeysTest {
      
          public interface Interwoven {
          }
      
          @Singleton
          public static class Classified implements Interwoven {
          }
      
          public static void main(String[] args) {
      
              Injector parent = Guice.createInjector(new AbstractModule() {
      
                  @Override
                  protected void configure() {
                      bind(Interwoven.class).to(Classified.class);
      
                      // Without this line child.getInstance(Classified.class) returns a new instance
                      bind(Classified.class);
                  }
              });
      
              System.out.println(parent.getInstance(Interwoven.class));
              System.out.println(parent.getInstance(Classified.class));
      
              Injector child = Guice.createInjector(new GuiceParentBindingsModule(parent));
      
              System.out.println(child.getInstance(Interwoven.class));
              System.out.println(child.getInstance(Classified.class));
          }
      }
      

      The problem is that the child injector only gets the explicit bindings from the parent and creates its own singleton when asked for Classified on the last line.

      Fixing this would mean adding support for multiple type declarations in module descriptors. It will also require restructuring the configuration objects used to represent the configuration and updating the code that actually adds them in Guice to support the multiple types.

      New format would be:

          <component>
            <type>info.magnolia.cms.i18n.MessagesManager</type>
            <type>info.magnolia.cms.i18n.AnotherInterface</type>
            <implementation>info.magnolia.cms.i18n.DefaultMessagesManager</implementation>
          </component>
      

        Acceptance criteria

              Unassigned Unassigned
              tmattsson Tobias Mattsson
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved:

                  Task DoD