Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6349987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:43:30+00:00 2026-05-24T21:43:30+00:00

I would like to bind a unique singleton instance for each annotation as shown

  • 0

I would like to bind a unique singleton instance for each annotation as shown below, such that Client1.a != Client1.b and Client1.a == Client2.a.

class X {}

@Singleton class OneOfEachAnnotation { 
   @Inject OneOfEachAnnotation(X x) { }
}

class Client1 {
   @Inject Client(@A OneOfEachAnnotation a, @B OneOfEachAnnotation b) {}
}

class Client2 {
   @Inject Client(@A OneOfEachAnnotation a, @B OneOfEachAnnotation b) {}
}

This answer seems to claim that I can accomplish the binding like this, but when I do so, Client1.a == Client1.b.

bind(OneOfEachAnnotation.class).annotatedWith(A.class).to(OneOfEachAnnotation.class).in(Singleton.class);
bind(OneOfEachAnnotation.class).annotatedWith(B.class).to(OneOfEachAnnotation.class).in(Singleton.class);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:43:31+00:00Added an answer on May 24, 2026 at 9:43 pm

    Thanks to Sam Berlin at Google Groups Guice forum. The reason this doesn’t work is because the OneOfEachAnnotation is already annotated with @Singleton. Removing the @Singleton annotation fixes the problem.

    Sam explained why:

    The statements:

    bind(OneOfEachAnnotation.class).annotatedWith(A.class).
     to(OneOfEachAnnotation.class).in(Singleton.class);
    bind(OneOfEachAnnotation.class).annotatedWith(B.class).
     to(OneOfEachAnnotation.class).in(Singleton.class);
    

    are saying, “Create a binding from @A OneOfEachAnnotation to OneOfEachAnnotation and make the @A OneOfEachAnnotation a singleton.” Same thing with @B.

    Ignoring the @Singleton on OneOfEachAnnotation for a second (assuming it didn’t exist), then this would have the behavior of:

    • User injects OneOfEachAnnotation (unannotated) multiple times: a new one is created each time, because that binding is unscoped.

    • User injects @A OneOfEachAnnotation, the first time it links down to the unannotated OneOfEach, sees it needs to be constructed, and constructs it. The second time the binding is already provisioned, so Scopes.SINGLETON doesn’t go to the linked binding again.
      (Same thing with @B as @A.)

    When OneOfEachAnnotation has @Singleton on it, then Guice thinks that the unscoped value is also a singleton. So when @A links to the unannotated, it will create the first instance. When @B links down the unannotated, Guice notices that the unannotated version, also a Singleton, has already been constructed, and returns that instance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom class that I would like to bind a WPF TreeView
I have a GridView that I would like to bind to an ObjectDataSource. I
I would like bind grid view with 2 different tables that has no relation.
I would like to bind a column in my DataGridView class to an entry
I would like to bind a TreeView control I have defined in XAML to
I would like to bind an Image to some kind of control an delete
I would like to bind C-` (control-backquote) but I could not do it. The
In a WPF based project I would like to bind the content of a
Hi I have a text field which I would like to bind to a
I would like to be able to programmatically bind some data to the dependency

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.