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

  • SEARCH
  • Home
  • 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 7801541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:58:53+00:00 2026-06-02T00:58:53+00:00

Guice provides two variations of so-called binding annotations , which seem to really break

  • 0

Guice provides two variations of so-called binding annotations, which seem to really break down to class- and instance-level annotations:

“Class-level”:

bind(Service.class).annotatedWith(Red.class).to(RedServiceImpl.class);

@Red
public class SomeService implements Service { ... }

Service redSvc = injector.getInstance(SomeService.class);

“Instance-level”:

bind(Service.class).annotatedWith(Names.named("Blue").to(BlueServiceImpl.class);
@Blue blueSvc = injector.getInstance(Service.class);

When is one method preferential over the other? It seems that class-level annotations are more absolute/inflexible than instance-level. Pros/cons/caveats/pitfalls of either method?

  • 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-06-02T00:58:54+00:00Added an answer on June 2, 2026 at 12:58 am

    I’m not sure I understand your question. Your use of binding annotations is irregular. You wouldn’t typically annotate a local variable or a class, but rather fields and parameters.

    Your first code example will cause the injector to return SomeService, but not because of your annotation or your binding, but because SomeService is a concrete implementation. Had you asked for this instead:

    Service redSvc = injector.getInstance(Service.class);
    

    You will get an error:

    1) No implementation for com.example.Service was bound.
      while locating com.example.Service
    

    Your second example is also incorrect. If you use Names to define a binding, you must use @Named to access that binding. Using @Blue would cause a compiler error. The correct usage would be @Named(value="Blue").

    The common best practice for a binding annotation is this:

    @BindingAnnotation
    @Target({ FIELD, PARAMETER, METHOD })
    @Retention(RUNTIME)
    public @interface MyAnno
    

    In that case, both of these would be compile errors:

    @Red // not allowed
    public class SomeService implements Service { ... }
    
    @Blue // not allowed
    blueSvc = injector.getInstance(Service.class);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd need something like @DefaultInstance(Level.NORMAL) enum Level {NORMAL, FANCY, DEBUGGING} which would make Guice
Google guice has a built-in logger binding . But what if I want to
I'm building an iOS app, which provides a service which our website already provides.
I have unit test, i use guice for di, i annotate my class with
I have a method annotated with @Provides in my Guice config, but it doesn't
Let's say I have a Closeable object injected through Guice using request scope: @Provides
I create guice servlet like this: public class GuiceApplicationServlet extends AbstractApplicationServlet { protected Provider<Application>
I struggled finding a how-to which provides a stable solution for using Qt with
i am new to solr. I have created two separate entities which are not
The following Java code sets up a Guice binding. It uses an anonymous subclass

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.