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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:23:00+00:00 2026-05-31T09:23:00+00:00

I’m with some doubts how to implement dependency injection with guice when exists multiple

  • 0

I’m with some doubts how to implement dependency injection with guice when exists multiple implementations of same interface and this dependency is defined at runtime based on parameters, so I’ll give an example to easily explain my question:

Imagine the scenario where you have one module to load files of multiple formats, basically you have one interface defining the contract, and multiple implementations one for each format:

public interface FileLoader {
    void load(File file);
}

public class YMLFileLoader{
    void load(File file){
    System.out.println("Loading YML");
    }
}

public class XMLFileLoader{
     void load(File file){
         System.out.println("Loading XML");
     }
}

Now, in runtime guice have to define based on file extension the implementation that must be used to load it.
My idea to maintain the code clean is make use of annotations, for each implementation is specified what she loads through @FileLoaderType annotation.

@Singleton
@FileLoaderType("yml")
public class YMLFileLoader{
    void load(File file)
    {
        System.out.println("Loading YML");
    }
}

@Singleton
@FileLoaderType("xml")
public class XMLFileLoader{
    void load(File file)
    {
        System.out.println("Loading XML");
    }
}

My first question is if implementation is possible?

Being the first question is positive, there is any way to implement this solution where for each new implementation of FileLoader doesn’t require refactoring in implementation of AbstractModule that supports the solution?
In other words, is basically for each new implementation of FileLoader only be required the existence of the annotation @FileLoaderType to Guice know what is the dependency it should inject if the extention match with her.

  • 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-31T09:23:01+00:00Added an answer on May 31, 2026 at 9:23 am

    One thing Guice can’t do is it can’t scan your classpath and find what classes you have, so you will need some way to tell guice what classes you have available. Therefore let’s split your question into two halves: getting a list of FileLoader implementation classes, and binding those classes into Guice.

    Let me tackle the second half first. I’ll assume that you have in your AbstractModule subclass a method called getFileLoaderClasses with the signature:

    private List<Class<? extends FileLoader>> getFileLoaderClasses() { ... }
    

    In that case, what I would recommend for binding the FileLoader implementations is something like this:

    private void bindFileLoaders() {
      MapBinder<String, FileLoader> mapBinder
          = MapBinder.newMapBinder(binder(), String.class, FileLoader.class);
      for (Class<? extends FileLoader> implClass : getFileLoaderClasses()) {
        FileLoaderType annotation = implClass.getAnnotation(FileLoaderType.class);
        if (annotation == null) {
          addError("Missing FileLoaderType annotation on " + implClass.getClass());
          continue;
        }
        mapBinder.addBinding(annotation.getValue()).to(implClass);
      }
    }
    

    This requires the guice-multibindings extension. Once you bind the implementations like that, you can use it as:

    public class MyLoader {
      @Inject Map<String, FileLoader> fileLoaderMap;
    
      public void load(File file, String type) {
        FileLoader fileLoader = fileLoaderMap.get(type);
        if (fileLoader == null) {
          throw new IllegalArgumentException("No file loader for files of type " + type);
        }
        fileLoader.load(file);
      }
    }
    

    So now, how do we get that full list of implementation classes? There are a few ways to do this:

    • Have one class with a static list of all the implementation classes:

      public class FileLoaderRegistry {
        public static final List<Class<? extends FileLoader>> impls =
            ImmutableList.of(
              YMLFileLoader.class,
              XMLFileLoader.class,
              JsonFileLoader.class
            );
      }
      

      This has the advantage that it’s probably the simplest solution to implement.
      It has a disadvantage that you’ll need to update this one file with each new implementation, and recompile it.

    • Have a text file with all the class names in it and load the classes with Class.forName() after reading all the lines of the file.

    • If you have the FileLoader implementations in different jar files, you can have a text file listing the names of the classes in each jar in a common location and then use System.getResources() to get an Enumeration of URLs pointing to each of the text files. Then read each file and load the class objects with Class.forName().

    • The most complicated option is to use the annotation processing tool as part of your compilation process, and use that to generate either text files or the registry class. That’s a separate question.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string

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.