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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:46:44+00:00 2026-06-13T18:46:44+00:00

I have created Guice binding annotations that allow me to bind two different instances

  • 0

I have created Guice binding annotations that allow me to bind two different instances of a class depending on the annotation e.g.:

bind(Animal.class).withAnnotation(Cat.class).toInstance(new Animal("Meow"));
bind(Animal.class).withAnnotation(Dog.class).toInstance(new Animal("Woof"));

I was hoping to be able to create a provider method that provides a List that is a dependency for one of my classes, but can’t figure out how to use the annotations for this:

@Provider
List<Animal> provideAnimalList() {
    List<Animal> animals = new ArrayList<Animal>();
    animals.add(@Cat Animal.class); // No, but this is what I want
    animals.add(@Dog Animal.class); // No, but this is what I want
    return animals;
}

So I was assuming that I would just be able to use the annotations in the argument to add() method of the List… but no.

How should I be approaching this? It seems to me it would be simpler simply to new the two instances of the Animal class and maybe this is not how the binding annotations were meant to be used.

I’d appreciate comments on the best use of the binding annotations in this scenario.

Thanks

  • 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-13T18:46:45+00:00Added an answer on June 13, 2026 at 6:46 pm

    If it is really what you want, here a working solution :

    public class AnimalModule extends AbstractModule {
        @Override
        protected void configure() {
            bind(Animal.class).annotatedWith(Cat.class).toInstance(new Animal("Meow"));
            bind(Animal.class).annotatedWith(Dog.class).toInstance(new Animal("Woof"));
        }
    
        @Provides
        List<Animal> provideAnimalList(@Cat Animal cat, @Dog Animal dog) {
            List<Animal> animals = new ArrayList<Animal>();
            animals.add(cat);
            animals.add(dog);
            return animals;
        }
    
        public static void main(String[] args) {
            List<Animal> animals = Guice.createInjector(new AnimalModule()).getInstance(Key.get(new TypeLiteral<List<Animal>>() {
            }));
            for (Animal animal : animals) {
                System.out.println(animal);
            }
        }
    }
    

    Annotations :

    @Retention(value = RetentionPolicy.RUNTIME)
    @BindingAnnotation
    public @interface Cat {
    }
    

    Output :

    Animal{sound='Meow'}
    Animal{sound='Woof'}
    

    However :

    • Don’t create specific annotations, seems unnecessary in that case. Use @Named instead,
    • You may consider Multibindings to solve that problem.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this web application that i have created using Wicket, Guice and JPA.
Using Guice, if I have @Inject @Named(light) Color light; I can use bind(Color.class).annotatedWith(Names.named(light)) .toInstance(new
I am following the guide at http://guides.rubyonrails.org/association_basics.html and I have created class Customer <
I have created some JQuery that will expand a div 'popup' on hover and
I have created a gstreamer plugin with an element inside that would generate some
I have created an Amazon EC2 Instance that provides Windows Server 2008 with SQL
I have created a login page using a nice guide that I found about
I have created a custom dialog box extending the DialogFragment class. As stated in
I have created several small applications that use my own DLL. The problem is,
I have created a pojo as below. package demo; public class HelloWorld { public

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.