Code Monkey home page Code Monkey logo

dropwizard-hk2bundle's Introduction

Dropwizard hk2 Bundle

Simple dropwizard bundle for autodiscovery and injection of dropwizard managed objects, tasks etc using hk2 integration

Features

  • Auto registration and injection for:
    • Healthchecks
    • Managed objects
    • Lifecycle listeners
    • Tasks
    • Commands
    • HK2 Binders
    • Other bundles
  • Hibernate validators injections
  • Jdbi DAOs injections
  • Support for injections before Jersey initialisation

Usage

Gradle

repositories {
    maven { url "https://jitpack.io" }
}
dependencies {
    compile 'com.github.alex-shpak:dropwizard-hk2bundle:0.4.1'
    compile 'org.glassfish.hk2:hk2-metadata-generator:2.4.0'
}

Maven

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>com.github.alex-shpak</groupId>
        <artifactId>dropwizard-hk2bundle</artifactId>
        <version>0.4.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.hk2</groupId>
        <artifactId>hk2-metadata-generator</artifactId>
        <version>2.4.0</version>
    </dependency>
</dependencies>

Code

Add bundle to your dropwizard application

bootstrap.addBundle(HK2Bundle.with(this).build());

All classes that you want to be discovered or injected should be annotated with @org.jvnet.hk2.annotations.Service annotation

@Service
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 has dropwizard-jdbi module in dependencies, add JDBIBinder to HK2Bundle configuration. Then you would be able to inject DAOs.

HK2Bundle hk2bundle = HK2Bundle.with(this)
        .bind(new JDBIBinder(config -> config.database))
        .build();
bootstrap.addBundle(hk2bundle);
@Inject
private MyDAO myDAO;

How it works

Hk2bundle initializes new ServiceLocator and binds found services into it. Then it sets created ServiceLocator as parent of jersey's ServiceLocator

environment.getApplicationContext().setAttribute(
    ServletProperties.SERVICE_LOCATOR, serviceLocator
);

After jersey initialisation services (if enabled) will be re-injected with new ServiceLocator

Without hk2 metadata generator

If you don't want to use metadata generator you can bind services manually using AbstractBinder supplied to bundle constructor

public class Binder extends AbstractBinder {

    @Override
    protected void configure() {
        bind(DatabaseHealthCheck.class)
            .to(HealthCheck.class)
            .in(Singleton.class);
    }
}
bootstrap.addBundle(HK2Bundle.with(this)
        .bind(new Binder())
        .build()
);

Licence

MIT

dropwizard-hk2bundle's People

Contributors

alex-shpak avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.