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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:42:55+00:00 2026-05-26T02:42:55+00:00

Question What criteria should be used when deciding between: specifying a dependency with an

  • 0

Question

What criteria should be used when deciding between:

  • specifying a dependency with an annotation, and
  • specifying a dependency with a more specific interface

Example

Suppose I have:

interface FooLoader {
    Foo loadById(long id);
}

class DBFooLoader implements FooLoader {
    ... jdbc etc. etc. ...
}

class CachingFooLoader implements FooLoader {
    ...
    @Inject
    public CachingFooLoader(FooLoader delegate) {
        this.delegate = delegate;
    }
    ...
}

Suppose I want to bind FooLoader to CachingFooLoader, I have [at least] two ways to wire this:

Use an annotation binding

Change:

public CachingFooLoader(FooLoader delegate)

to:

public CachingFooLoader(@NonCaching FooLoader delegate)

and then:

bind(FooLoader.class).annotatedWith(NonCaching.class).to(DBFooLoader.class);

Create a more specific interface

Change:

public CachingFooLoader(FooLoader delegate)

to:

public CachingFooLoader(NonCachingFooLoader delegate)

where NonCachingFooLoader simply extends FooLoader, and then have DBFooLoader implement NonCachingFooLoader, and wire up accordingly.

My thoughts

I am drawn to using an annotation binding for multiple reasons:

  • Keys can be more easily reused than interfaces, which decreases the combinatorial explosion that interfaces would suffer from.
  • It is less invasive: configuration stays in Guice modules, rather than “poisoning” classes.

However, creating a more specific interface has its advantages too:

  • Interfaces have more meaning. Typically only Guice will read the annotation, where as interfaces are used for much more.

So, what criteria should be used to determine which approach to take?

(Spring users, as far as I can tell, this is what you guys call qualifiers.)

  • 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-26T02:42:55+00:00Added an answer on May 26, 2026 at 2:42 am

    Use specific interfaces only if it makes sense, i.e. they have to offer a different API and thus other classes will use them in a specific way.

    If they offer the same “service” in different ways, then use only one common interface and differentiate implementations with annotations.

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

Sidebar

Related Questions

I have a question about Criteria method, one-to-many relation to the database, 'one' is
I have what may be a pretty elementary Hibernate question. Do HQL (and/or Criteria)
I have a question related to C++ design. What are some criteria that you
I have a question for how to implement multiple criteria for a repository pattern
I am new to Stack Overflow and have a question about JSF 2.0 and
This seems like a pretty simple question, but I have not managed to find
At work, we have optimization engines, and one of the inputs used by these
This is more of a design than implementation question and it's going to be
Please ignore code readability in this question. In terms of performance, should the following
I have a content that I'd like to rate on multiple criteria. Imagine this

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.