Mimicking Spring profiles in Guice

guice, java

Solution

You'll need to identify the environment yourself, and choose which modules to apply in which environment, but you do have access to `Modules.override` to specifically override certain bindings without having to create a lot of module variants. Use it judiciously—it's very easy to get your modules tangled if you override many bindings or in unpredictable places.

Problem

In Spring, if I want to have one set of objects for production, and another for local development/testing. I could use the `@Profile` annotation to designate the different classes, and switch between them by providing a system property when starting the app. Is there anything like this in Guice, or do I need to manually check some property myself and load a different set of modules when bootstrapping my `Injector`?

Original source