Support for modules allows us to wire dependencies across multiple modules (jars). For example, we can have some common functionality (e.g. "feature-toggle") that we build as a module (jar) and we want to build other modules that reuse that module. These other modules can get the "feature toggle beans(s)" injected into them.

Module names

By default a module name is derived from the top most package. It is advisable to explicitly name modules that have common functionality that we want to depend on.

@ContextModule(name = "feature-toggle")

We use the @ContextModule annotation to give a module an explicit name.

Module dependsOn

Modules that depend on functionality / beans from other modules specify in dependsOn the modules that should provide beans they want to inject.

@ContextModule(name = "job-system", dependsOn = {"feature-toggle"})

With the above example the "job-system" module expects bean(s) from the "feature-toggle" module to be available for injecting into it's bean graph.

For DInject internally this defines the order in which the bean contexts in each of the modules are created. DInject finds all the modules in the classpath (via Service loader) and then orders the modules based on the dependsOn values. In the example above the "feature-toggle" bean context must be built first, and then the beans it contains are available when building the "job-system" bean context.