Skip to content
This repository has been archived by the owner on Sep 13, 2021. It is now read-only.

Latest commit

 

History

History
104 lines (79 loc) · 2.54 KB

README.md

File metadata and controls

104 lines (79 loc) · 2.54 KB

Dropwizard hk2 Bundle

Release

Dropwizard bundle for injection of managed objects, tasks etc using hk2 integration

Motivation

HK2 is default DI container in jersey, which is widely used in dropwizard, therefore it seems logical to use single DI container.

Features

  • Registration and injection for:
    • Healthchecks
    • Managed objects
    • Lifecycle listeners
    • Tasks
    • Commands
    • Metrics
    • Other bundles
  • Jdbi DAOs injections
  • Support for injections before Jersey initialisation

Usage

Grab latest version from JitPack

Gradle

repositories {
    maven { url "https://jitpack.io" }
}
dependencies {
    compile 'com.github.alex-shpak:dropwizard-hk2bundle:0.8.1'
}

Code

Add bundle to your dropwizard application

ExampleAppBinder appBinder = new ExampleAppBinder();

HK2Bundle hk2Bundle = new HK2Bundle(appBinder);
bootstrap.addBundle(hk2Bundle);

Register DatabaseHealthCheck in HK2 binder

public class ExampleAppBinder extends DropwizardBinder {

    @Override
    protected void configure() {
        healthCheck(DatabaseHealthCheck.class);
    }
}

DropwizardBinder is convenience class that contains register() method to register objects specific for dropwizard So now DatabaseHealthCheck get created and initialized by DI container

@Named("database") //Health check name. Class name will be used if not set 
public class DatabaseHealthCheck extends HealthCheck {

    @Inject 
    private Provider<Database> database;

    @Override
    protected Result check() throws Exception {
        if(database.get().isConnected()) {
            return Result.healthy();
        }
        return Result.unhealthy("Not connected");
    }
}

JDBI DAO Injection

Considering you already have dropwizard-jdbi module in dependencies, add JDBIBinder to HK2Bundle configuration. Then you would be able to inject DAOs.

ExampleAppBinder appBinder = new ExampleAppBinder();
JDBIBinder jdbiBinder = new JDBIBinder<ExampleAppConfiguration>(configuration -> configuration.database)
        // .setDBIFactory(JDBIFactory.class)
        // .setSqlObjectFactory(SqlObjectFactory.class)
        .register(ExampleDAO.class);


HK2Bundle hk2Bundle = new HK2Bundle(appBinder, jdbiBinder);
bootstrap.addBundle(hk2Bundle);
@Inject
private ExampleDAO exampleDAO;

Licence

MIT