Search code examples
dependency-injectionjersey-2.0dropwizardhk2

Registering Dropwizard configuration with Jersey 2 (HK2) DI


In my Dropwizard (1.2.4) application I'm having trouble injecting my Dropwizard configuration into classes that are instantiated by HK2. What's the best way to achieve this?


Solution

  • Just bind the configuration instance.

    @Override
    public void run(final DummyConfiguration conf, Environment env) {
        env.jersey().register(new AbstractBinder() {
            @Override
            public void configure() {
                bind(conf).to(DummyConfiguration.class);
            }
        })
    }
    

    Now you can @Inject the DummyConfiguration anywhere you need it.