question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multibinder Bindings Don't Always Respect Singleton Scope

See original GitHub issue

From nachtrabe on December 11, 2013 13:10:31

Description of the issue: When using the multibinder, if you use binder.addBinding().to(B.class).in(Singleton.class); it will not enforce the singleton nature on that object, but using the @`Singleton` annotation directly will. Steps to reproduce: 1. Create a Multibinder.newSetBinder instance with an interface. 2. Bind an implementation using `.in(Singleton.class)`. 3. Bind another implementation annotated with @Singleton. 4. Inspect that the instances retrieved are not singletons in the cases where .in(Singleton.class) was used, but are in the cases where ``@Singleton was used.

Expected behavior:

- I’d expect for .in(Singleton.class) to work, to not be exposed, or to throw an exception.

The attached tests illustrate the problem: getInstance_ofSet_ContainsB and getInstance_ofB_IsIdentical both fail under both Guice 3.0 and Guice 4.0-beta.

Attachment: gist    GuiceTest.java

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

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
gjosephcommented, Mar 2, 2015

Sam and OP, I got this to work this way: (with help from @tmattsson) Op’s original binding:

            binder.addBinding().to(B.class).in(Singleton.class);

replaced by this:

            binder().bind(B.class).in(Singleton.class);
            binder.addBinding().to(Key.get(B.class));

makes it work as we expect. WDYT ?

0reactions
gjosephcommented, Feb 26, 2015

Sure; that works if I have a Module in that same package. I’m dealing with a ball of legacy code, and can’t really do that (or ensure that everyone does it, anyway). That said, the extra binding is also probably no big deal, but I have no way of checking (in plugin-based environment, my only option now is to let the bindings fail at runtime, and it might not fail for everyone)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Re: Issue 791 in google-guice: Multibinder Bindings Don't Always ...
Re: Issue 791 in google-guice: Multibinder Bindings Don't Always Respect Singleton Scope ... You received this message because this project is configured to...
Read more >
[guice] Multibinder Bindings Don't Always Respect Singleton Scope ...
Is there no way around this ? (e.g is it unreasonable to _not_ want to explicitly bind using ```FooImpl``` as a key, or...
Read more >
Guice multibinding-instances get different dependency ...
It's worth noting that Guice's behaviour in this respect is different from Spring's. Spring DI treats all beans as singletons by default.
Read more >
Detecting the scope associated with a binding? - Google Groups
Hi, Is it possible to detect the scope associated with Binding returned by. Injector.getBinding(Key)? I'm trying to detect at runtime whether a
Read more >
目录 - Gitee
Inspecting Multibindings or MapBindings (new in Guice 3.0) ... Usually you'll also bind the scope itself, so interceptors or filters can use it....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found