[MAGNOLIA-4471] Support for specifying multiple types on a component Created: 06/Jul/12  Updated: 19/May/22  Resolved: 19/May/22

Status: Closed
Project: Magnolia
Component/s: core
Affects Version/s: 4.5.3
Fix Version/s: None

Type: Improvement Priority: Neutral
Reporter: Tobias Mattsson Assignee: Unassigned
Resolution: Won't Do Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
dependency
Template:
Acceptance criteria:
Empty
Task DoD:
[ ]* Doc/release notes changes? Comment present?
[ ]* Downstream builds green?
[ ]* Solution information and context easily available?
[ ]* Tests
[ ]* FixVersion filled and not yet released
[ ]  Architecture Decision Record (ADR)
Date of First Response:

 Description   

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>


 Comments   
Comment by Roman Kovařík [ 19/May/22 ]

Hello,

This ticket is now marked as closed due to one of the following reasons:

  • A long period of inactivity
  • Uses an old or Beta version of an application, module, or framework that we no longer support
  • The issue is no longer reproducible or has been fixed in later versions

If you are still facing a problem or consider this issue still relevant, please feel free to re-open the ticket and we will reach out to you.

Thank you,
The Magnolia Team

Generated at Mon Feb 12 03:56:06 CET 2024 using Jira 9.4.2#940002-sha1:46d1a51de284217efdcb32434eab47a99af2938b.