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.
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
AbstractModulesubclass a method calledgetFileLoaderClasseswith the signature:In that case, what I would recommend for binding the
FileLoaderimplementations is something like this:This requires the guice-multibindings extension. Once you bind the implementations like that, you can use it as:
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:
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
FileLoaderimplementations 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 useSystem.getResources()to get anEnumerationofURLs pointing to each of the text files. Then read each file and load the class objects withClass.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.