Skip to content

Unexpected interaction between InjectionListener and singleton bindings #802

@gissuebot

Description

@gissuebot

From lorenzo@intelerad.com on April 08, 2014 14:35:22

I apologize if this issue is already known. I only found 2 issues mentioning the InjectionListener and none seemed related to the following. Description of the issue: It seems that instantiating singleton instances within the scope of InjectionListener.afterInjection() causes Guice to associate an instance to a type instead of associating it to a key (type and annotation).

The attached application InjectorListenerWithSingletons attempts to illustrate the issue:

* Bind an annotated interface to an implementation as a singleton.

  • Bind the same interface with a different annotation to the same implementation as a singleton.
  • Retrieve instances of both annotated interfaces and compare them (using ==)
  • Repeat the same steps but this time instantiating the annotated interfaces within an InjectionListener.afterInjection() Steps to reproduce: 1. Execute InjectorListenerWithSingletons as is with java assertions enabled. It should fail with error "KEY_1 and KEY_2 should be different".
  1. Comment out line 63 (p1.get()) or line 64 (p2.get()), or both, and re-run the application. This time the test should pass.

Let me know if there is a fundamental flaw in the design of the test, or if you need more details or additional tests.

Thanks for your time.

Attachment: gist
   InjectorListenerWithSingletons.java

Original issue: http://code.google.com/p/google-guice/issues/detail?id=802

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions