Bootstrap

In order to allow the same application code to run in both Vertx and Servlet environments, a bootstrap sequence is needed.

This was originally enables the Guice Injector to be held in a location suitable to the environment, so that it can be retrieved during deserialisation

It also sets the value of the @RuntimeEnvironment option, so that the application may make other adjustments if needed.

Injector Location

In a standard Servlet environment, a static variable is the simplest way to hold a reference to the Guice Injector. This is provided by InjectorHolder, but should usually only be accessed through InjectorLocator - this enables the code to be application portable, because the InjectorLocator implementation is environment specific.

In a Vertx environment, the injector instance is held in Vertx.currentContext. Again, to ensure portability, access should be through InjectorLocator

Guice Bindings

Environment specific bindings are defined in ServletEnvironmentModule and VertxEnvironmentModule, for:

  • InjectorLocator
  • SerializationSupport

Bootstrap file

This is described in the User Guide <../userguide/userguide-bootstrap>`

Detecting the Environment

The Bootstrap process provides detection of the runtime environment, which can be accessed by Guice injection

Java
public class MyClass {

   @Inject
   protected MyClass(@RunningOn RuntimeEnvironment runtimeEnvironment){

   }
}
Kotlin
class MyClass @Inject constructor(@RunningOn runtimeEnvironment:RuntimeEnvironment)