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

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-shpak committed Nov 7, 2018
1 parent 82e3c51 commit 26e2c5a
Showing 1 changed file with 16 additions and 27 deletions.
43 changes: 16 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ Dropwizard bundle for injection of managed objects, tasks etc using hk2 integrat
## Motivation
HK2 is default DI container in jersey, which is widely used in dropwizard, therefore it seems logical to use single DI container.

## TODO!!!
Add docs about validation bundle registration

## Features
- Registration and injection for:
Expand All @@ -17,7 +15,6 @@ Add docs about validation bundle registration
- Commands
- Metrics
- Other bundles
- Hibernate validators injections
- Jdbi (version 2) DAOs injections
- Support for injections before Jersey initialisation

Expand All @@ -33,24 +30,23 @@ repositories {
```
```groovy
dependencies {
compile 'com.github.alex-shpak:dropwizard-hk2bundle:0.6.5'
compile 'com.github.alex-shpak:dropwizard-hk2bundle:0.8.0'
}
```


#### Code
Add bundle to your dropwizard application
```java
HK2Bundle hk2bundle = HK2Bundle.builder()
.addBinder(new Binder())
.build();
ExampleAppBinder appBinder = new ExampleAppBinder();

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

Register `DatabaseHealthCheck` in HK2 binder
```java
public class Binder extends DropwizardBinder {
public class ExampleAppBinder extends DropwizardBinder {

@Override
protected void configure() {
Expand Down Expand Up @@ -82,32 +78,25 @@ public class DatabaseHealthCheck extends HealthCheck {


#### JDBI DAO Injection
Considering you already has `dropwizard-jdbi` module in dependencies, add `JDBIBinder` to `HK2Bundle` configuration.
Considering you already have `dropwizard-jdbi` module in dependencies, add `JDBIBinder` to `HK2Bundle` configuration.
Then you would be able to inject DAOs.

```java
HK2Bundle hk2bundle = HK2Bundle.builder()
.jdbi(config -> config.database, Configuration.class)
.build();
bootstrap.addBundle(hk2bundle);
```
```java
@InjectDAO
private MyDAO myDAO;
```
ExampleAppBinder appBinder = new ExampleAppBinder();
JDBIBinder jdbiBinder = new JDBIBinder<ExampleAppConfiguration>(configuration -> configuration.database)
// .setDBIFactory(JDBIFactory.class)
// .setSqlObjectFactory(SqlObjectFactory.class)
.register(ExampleDAO.class);


## How it works
Hk2bundle initializes new `ServiceLocator` and binds found services into it.
Then it sets created `ServiceLocator` as parent of jersey's `ServiceLocator`
HK2Bundle hk2Bundle = new HK2Bundle(appBinder, jdbiBinder);
bootstrap.addBundle(hk2Bundle);
```
```java
environment.getApplicationContext().setAttribute(
ServletProperties.SERVICE_LOCATOR, serviceLocator
);
@Inject
private ExampleDAO exampleDAO;
```

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


## Licence
[MIT](LICENSE)

0 comments on commit 26e2c5a

Please sign in to comment.