Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The guice AbstractModule install method

Tags:

java

guice

What does the method install() from the AbstractModule class do? Can someone explain it to me? From the docs I read from the guice site all I could get was:

Uses the given module to configure more bindings.

Configure what bindings exactly? The bindings from the installed module or the bindings of the class that invoked the install method?

like image 430
MykelXIII Avatar asked Jan 30 '14 02:01

MykelXIII


People also ask

What does install in Guice do?

install allows for composition: Within its configure method, FooModule may install FooServiceModule (for instance). This would mean that an Injector created based only on FooModule will include bindings and providers in both FooModule and FooServiceModule.

What is AbstractModule?

public abstract class AbstractModule extends java.lang.Object implements Module. A support class for Module s which reduces repetition and results in a more readable configuration. Simply extend this class, implement configure() , and call the inherited methods which mirror those found in Binder .

Why we use Google Guice?

Beyond Dependency Injection, the benefits of using Google Guice is: Guice has a very clean implementation of constructor Injection. As you can see from the example you just add @Inject annotation constructor. Guice also has setter Injection using the same annotation.

What is module in Guice?

The Module is the core building block for an Injector which is Guice's object-graph builder. First step is to create an injector and then we can use the injector to get the objects. public static void main(String[] args) { /* * Guice.createInjector() takes Modules, and returns a new Injector * instance.


1 Answers

install allows for composition: Within its configure method, FooModule may install FooServiceModule (for instance). This would mean that an Injector created based only on FooModule will include bindings and providers in both FooModule and FooServiceModule.

You may see install used to split a Module into logical sub-modules for ease of readability or testing, or for a high-level Module to ensure its dependencies are configured. You might also use it to instantiate module instances with different constructor parameters (binding multiple data stores, for instance), or to install automatically-generated module instances like those created through FactoryModuleBuilder.

Module composition can be a double-edged sword, because duplicate bindings are not allowed: If your FooModule and BarModule both install the same dependent module, and the bindings are not exact duplicates (e.g. if a Module instantiates an object in its configure method), Guice will fail to create any Injector that installs both FooModule and BarModule due to the duplicate binding. You could work around this by defining equals and hashCode on your Modules, or by managing your composition such that any Module is either top-level or installed in exactly one other Module (but never both).

See this archived blog or this SO answer for more on de-duplicating bindings.

like image 145
Jeff Bowman Avatar answered Oct 07 '22 03:10

Jeff Bowman