com.gruelbox:dropwizard-guice-box-hibernate

Dropwizard/Guice integration bundle Hibernate Support

License

License

Categories

Categories

DropWizard Container Microservices GUI User Interface Guice Application Layer Libs Dependency Injection Hibernate Data ORM
GroupId

GroupId

com.gruelbox
ArtifactId

ArtifactId

dropwizard-guice-box-hibernate
Last Version

Last Version

1.1.14
Release Date

Release Date

Type

Type

jar
Description

Description

com.gruelbox:dropwizard-guice-box-hibernate
Dropwizard/Guice integration bundle Hibernate Support
Project Organization

Project Organization

Gruelbox

Download dropwizard-guice-box-hibernate

How to add to project

<!-- https://jarcasting.com/artifacts/com.gruelbox/dropwizard-guice-box-hibernate/ -->
<dependency>
    <groupId>com.gruelbox</groupId>
    <artifactId>dropwizard-guice-box-hibernate</artifactId>
    <version>1.1.14</version>
</dependency>
// https://jarcasting.com/artifacts/com.gruelbox/dropwizard-guice-box-hibernate/
implementation 'com.gruelbox:dropwizard-guice-box-hibernate:1.1.14'
// https://jarcasting.com/artifacts/com.gruelbox/dropwizard-guice-box-hibernate/
implementation ("com.gruelbox:dropwizard-guice-box-hibernate:1.1.14")
'com.gruelbox:dropwizard-guice-box-hibernate:jar:1.1.14'
<dependency org="com.gruelbox" name="dropwizard-guice-box-hibernate" rev="1.1.14">
  <artifact name="dropwizard-guice-box-hibernate" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.gruelbox', module='dropwizard-guice-box-hibernate', version='1.1.14')
)
libraryDependencies += "com.gruelbox" % "dropwizard-guice-box-hibernate" % "1.1.14"
[com.gruelbox/dropwizard-guice-box-hibernate "1.1.14"]

Dependencies

compile (2)

Group / Artifact Type Version
com.gruelbox : dropwizard-guice-box jar 1.1.14
io.dropwizard : dropwizard-hibernate jar 2.0.15

Project Modules

There are no modules declared in this project.

dropwizard-guice-box

Maven Central Javadocs CD CodeFactor

A slightly different take on Guice integration with DropWizard, which makes heavy use of multibindings to create a more plugin-like, modular experience.

Installation

Check the latest release and add the dependency to your POM:

<dependency>
  <groupId>com.gruelbox</groupId>
  <artifactId>dropwizard-guice-box</artifactId>
  <version>latest release version</version>
</dependency>

All releases from 1.0.0 onwards are semantically versioned. You can safely take any release incrementing the minor or patch versions without worrying about compatibility.

Usage

public class MyApplication extends Application<MyConfiguration> {

  // Optional - you can inject directly into your Application to get access
  // to injected assets in the run() method.
  @Inject private SomethingINeed somethingINeed;

  @Override
  public void initialize(final Bootstrap<MyConfiguration> bootstrap) {
    super.initialize(bootstrap);
    
    // Any modules listed here can implement Configured<MyConfiguration> to get access to the
    // configuration during the injector build.
    bootstrap.addBundle(
      new GuiceBundle<MyConfiguration>(
        this,
        new MyApplicationModule(),
        new MyOtherApplicationModule()
      )
    );
  }

  @Override
  public void run(final MyConfiguration configuration, final Environment environment) {
    // You can access injected assets here if you wish, however, multi-binding
    // an EnvironmentInitialiser is a cleaner way to do this, allowing your code to
    // be more modular.
    somethingINeed.canNowBeUsed();
  }
}

You now have access to some Guice idiomatic bind points in your Modules:

  • Multibindings to WebResource provide resources.
  • Multibindings to HealthCheck provide healthchecks.
  • Multibindings to ExceptionMapper allow you to map exceptions from your endpoints to HTTP responses.
  • Multibindings to Task to provide administration tasks.
  • Multibindings to Managed provide controlled startup and shutdown of components.
  • Multibindings to Service provide controlled startup and shutdown of background processes.
  • Multibindings to EnvironmentInitialiser allow you to hook into the Application.run phase directly from anywhere in your code when you need to do anything else on startup, without having to add code to Application.run directly.

All of these can inject as normal, and all except WebResource can be package private, aiding encapsulation (unfortunately, JAX-WS prevents this for JAX-RS resources).

The following are provided by default for injection anywhere:

  • Your application configuration
  • The Environment
  • The HttpServletRequest and HttpServletResponse during web requests (thanks to GuiceFilter), if you install new ServletModule() during Injector creation.

In addition, any Module provided directly to GuiceBundle can support the Configured interface to gain access to the application configuration during bind time. This can be helpful where your configuration drives the choice of bindings.

Hibernate

If you're using Dropwizard's Hibernate bundle, you can provide your @Entity classes via injection too, which is great for keeping underlying database structure encapsulated in your modules.

Add the dependency:

<dependency>
  <groupId>com.gruelbox</groupId>
  <artifactId>dropwizard-guice-box-hibernate</artifactId>
  <version>1.1.0</version>
</dependency>

And modify your code to construct and install the HibernateBundle, replacing the example DataSourceFactory configuration in the example with your requirements:

public class MyApplication extends Application<MyConfiguration> {

  @Inject private SomethingINeed somethingINeed;

  @Override
  public void initialize(final Bootstrap<MyConfiguration> bootstrap) {
  
    super.initialize(bootstrap);
    
    HibernateBundleFactory<MyConfiguration> hibernateBundleFactory = new HibernateBundleFactory<>(configuration -> {
      DataSourceFactory dsf = new DataSourceFactory();
      dsf.setDriverClass(configuration.getDriverClass());
      dsf.setUrl(configuration.getJdbcUrl());
      // etc...
      return dsf;
    });
    
    bootstrap.addBundle(
      new GuiceBundle<MyConfiguration>(
        this,
        new MyApplicationModule(),
        new MyOtherApplicationModule(),
        new GuiceHibernateModule(hibernateBundleFactory)
      )
    );
    
    bootstrap.addBundle(hibernateBundleFactory.bundle());
  }

  @Override
  public void run(final MyConfiguration configuration, final Environment environment) {
    somethingINeed.canNowBeUsed();
  }
}

You now have access to both SessionFactory and HibernateBundle via injection, and can multi-bind implementations of EntityContribution to provide JPA entities at runtime, as you would with WebResource, Healthcheck etc.

Examples

See the tests for some basic examples.

com.gruelbox

The Gruelbox

Simple, nutritious open source tools.

Versions

Version
1.1.14
1.1.13
1.1.10
1.1.7
1.1.0
1.0.0
0.0.8
0.0.7
0.0.6
0.0.5
0.0.4
0.0.3
0.0.2