28

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?
IAmYourFaja
  • 50,141
  • 159
  • 435
  • 728

1 Answers1

15

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.

Peter Coulton
  • 50,723
  • 11
  • 51
  • 70
th3morg
  • 3,782
  • 28
  • 44
  • Thanks @th3morg (+1) - I guess its *starting* to make sense, and I'll absolutely look at the sources to do a bit more digging. One followup question about `Environment`: would this be used for, say, determining that we're in a "dev" version of the Jersey/Jetty, and so we should use an in-memory embedded db, vs a "prod" version of the container, hence use our actual MySQL server? If not, can you cite a few specific examples of `Environment`'s use cases? Thanks again! – IAmYourFaja Dec 09 '14 at 22:49
  • 1
    @IAmYourFaja One other thing I recommend doing is running your application in debug mode in an IDE where you can pause and/or step through the execution through the Bootstrap and Environment classes. You'll quickly get an idea of all of the work being done behind the scenes to configure everything :) – th3morg Dec 10 '14 at 07:48
  • Thanks @th3morg (+1) - but I'm on Windows where I don't have access to an IDE, I'm just using plain ole' Notepad. – IAmYourFaja Dec 10 '14 at 12:42
  • If you want, you can get NetBeans or Eclipse or even IntelliJ Community all on windows – th3morg Dec 10 '14 at 16:14
  • 6
    I really don`t understand why Bootstrap class is parametrized with Configuration, but there is no way to access it while preparing bundles for initialization (i tend to use Guice in initialize() method to inject dependencies into bundles and need access to config there) – macromaniac Feb 23 '16 at 20:20