google / google/guice

Unexpected interaction between InjectionListener and singleton bindings

Open
#802 0 comments 0 reactions 0 assignees View on GitHub
imported
Dominant language
Java
Stars
12.7k
Forks
1.7k
Avg merge
11m
Merged PRs (30d)
2

Description

_From [lorenzo@intelerad.com](https://code.google.com/u/115220922512671062157/) 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".
2. 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](https://gist.github.com/c38119b4347d5ede63ce)
   _[InjectorListenerWithSingletons.java](https://gist.githubusercontent.com/gissuebot/c38119b4347d5ede63ce/raw/20b10c8f270e42a1f941225ec93e33f24349bc13/InjectorListenerWithSingletons.java)_

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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.