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 6650807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:56:11+00:00 2026-05-26T00:56:11+00:00

I have an interface: InterfaceA . I have a class: ConcreteA . I also

  • 0

I have an interface: InterfaceA.

I have a class: ConcreteA.

I also have two annotations: @AnnotA and @AnnotB.

I have done the following bindings:

bind(InterfaceA).annotatedWith(AnnotA).to(ConcreteA);
bind(InterfaceA).annotatedWith(AnnotB).to(ConcreteA);

Next, class ConcreteA has a constructor that takes a String argument called hostName.

class ConcreteA
{
    @Inject
    public ConcreteA(@Named("hostName") hostName) {
    }

    ... <rest of class>
}

I need code to describe the following:

If ConcretaA is using @AnnotA then bind hostName with String value of ‘localhost’

If ConcreteA is using @AnnotB then bind hostName with String value of ‘externalhost’

Any ideas of a solution for this?

  • 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-26T00:56:11+00:00Added an answer on May 26, 2026 at 12:56 am

    I think in your case, you might consider putting each binding in its own private module.

    class MyModule() { 
      install(new PrivateModule() {
        public void configure() {
           bind(InterfaceA).to(ConcreteA);
           bind(String.class).annotatedWith(Names.named("hostName").to("localhost");
           expose(InterfaceA).annotatedWith(AnnotA.class);
        }});
      install(new PrivateModule() {
        public void configure() {
           bind(InterfaceA).to(ConcreteB);
           bind(String.class).annotatedWith(Names.named("hostName").to("externalhost");
           expose(InterfaceA).annotatedWith(AnnotB.class);
        }});
    }
    

    (This is from memory and syntax may not be 100% correct.)

    For more detail, start with the Guice FAQ, and search that page for “robot legs” — I’m not joking 🙂

    There is even more detail behind the two additional links from that section of the FAQ.

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

Sidebar

Related Questions

I have following simple class: @interface Article: NSObject { NSString *title; } @property (copy,
Have a interface class abc { public: virtual int foo() = 0; ... }
I have an interface: public interface IMyInterface { } I have a class which
I have an interface: public abstract class Authorizer<T> where T : RequiresAuthorization { public
Say I have my class @interface Person : NSObject { NSString *name; } I
Suppose we have: interface Foo { bool Func(int x); } class Bar: Foo {
I have a class A, and B like these: @interface A : UIViewController <UITableViewDelegate,
I have an interface with several events I have base class implementing the interface
I have the interface defined as below and is implemented by a single class
I have an interface, IMessage and a class which have several methods for creating

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.