DropWizard Bootstrap vs Configuration vs Environment

IAmYourFaja picture IAmYourFaja · Dec 9, 2014 · Viewed 10.8k times · Source

A typical DropWizard application specifies an Application subclass, and override its initialize method like so:

class MyApplication extends Application<MyConfiguration> {
    static void main(String[] args) {
        new MyApplication().run(args)
    }

    @Override
    public void initialize(Bootstrap<MyConfiguration> bootstrap) {
        // ???
    }

    @Override
    public void run(MyConfiguration configuration, Environment environment)
            throws Exception {
        // Register resources, health checks, etc.
    }
}

After perusing the DropWizard docs, as well as the JavaDocs for:

  • Configuration - An object representation of the YAML configuration file. Extend this with your own configuration properties, and they'll be parsed from the YAML file as well.
  • Bootstrap - The pre-start application environment, containing everything required to bootstrap a Dropwizard command.
  • Environment - A Dropwizard application's environment.

But these are rather vague class definitions, particularly the last two. I understand that I am supposed to subclass Configuration, and that it represents an in-memory POJO of my app's YAML/JSON config file.

But I can not understand what the other constructs represent (Bootstrap and Environment). I am used to injecting environment-specific configs into my apps, and so I tend to think of the concepts of "environment" and "configuration" as one in the same.

Furthermore, it seems DropWizard closely couples Bootstrap instances with Configuration impl instances, but I can find no demonstrable examples as to how these two classes are different, and how they should be used different from one another.

So I ask:

  1. What is a Bootstrap, what do I use it for?
  2. What is an Environment, and what do I use it for?

Answer

th3morg picture th3morg · Dec 9, 2014

Dropwizard is basically an opinionated web framework, primarily used for serving as a REST API project. The classes you're asking about are the crux of what makes a Dropwizard application. The developers have combined all of the libraries they want used in their framework and wired them together so that we can easily work off of what they've bootstrapped for us.

Environment is the Dropwizard Environment container, not your application's personal environment (i.e. local vs. production). It has properties that are core to the Dropwizard framework such as the jersey web container.

Bootstrap is basically the class that wires up everything being used in the Environment, including your Configuration and Application.

If you have a look at the source files, you'll get a good idea of how these classes are working.

Update: Per your question below, the Environment shouldn't used to determine database connection types and credentials; that is what your Configuration .yml file is for. You'll want to put any environment specific variables in that file and then run your application with a specific .yml file. I personally have an application-local.yml, application-staging.yml and application-prod.yml and run my application with the appropriate .yml depending on the environment.

Dropwizard does some auto configuration of datasources with specific .yml properties: see here.