I am currently creating a Dropwizard + Guice + Jersey application where database access is currently being processed by JDBI.
What I'm trying to achieve is to have a typical corporate architecture, where resources access classes of service that access the DAO class, which in turn accesses the database. It would be nice to get all this in the correct DI order, although I think I can build my object graph in the run () method of the application if all else fails.
So, I ran into this problem, which was mentioned here before : Getting DBIFactory requires both an environment and a configuration that somehow needs to be available at the time that Guice is doing magic for injection, and not at runtime () time .
Being Dropwizard and Guice noob, I managed to collect so far that I need a provider for my DAO objects, something like a melody
public class UserDAOProvider implements Provider<UserDAO> { @Inject Environment environment; @Inject Configuration configuration; @Override public UserDAO get() { final DBIFactory factory = new DBIFactory(); final (MyConfiguration) config = (MyConfiguration) configuration; DBI jdbi = null; try { jdbi = factory.build(environment, config.getDataSourceFactory(), "mysql"); } catch (ClassNotFoundException e) {
By registering this as a singleton provider, I must then enter UserDAO into my services.
Now, how do we actually get the environment embedded in the Provider? I am currently stuck in Guice complaining that I did not find a suitable constructor for the environment, so it is trying to create an instance and not grab it from Dropwizard itself.
This seems to be doable; There is a dropwizard-guice package, DropWizardEnvironmentModule, I think I need. But I feel like I just missed some piece of the puzzle here to figure out how to stack things. So far I have not been able to find a complete working example ...