相关疑难解决方法(0)

如何将jdbiFactory DAO注入Dropwizard命令?

我开始使用Dropwizard,我正在尝试创建一个需要使用数据库的Command.如果有人想知道我为什么要这样做,我可以提供充分的理由,但无论如何这不是我的问题.它是关于Dropwizard中的依赖性反转和服务初始化以及运行阶段.

Dropwizard鼓励使用它的DbiFactory来构建DBI实例,但是为了得到一个,你需要一个Environment实例和/或数据库配置:

public class ConsoleService extends Service<ConsoleConfiguration> {

  public static void main(String... args) throws Exception {
    new ConsoleService().run(args);
  }

  @Override
  public void initialize(Bootstrap<ConsoleConfiguration> bootstrap) {
    bootstrap.setName("console");
    bootstrap.addCommand(new InsertSomeDataCommand(/** Some deps should be here **/));
  }

  @Override
  public void run(ConsoleConfiguration config, Environment environment) throws ClassNotFoundException {
    final DBIFactory factory = new DBIFactory();
    final DBI jdbi = factory.build(environment, config.getDatabaseConfiguration(), "postgresql");
    // This is the dependency I'd want to inject up there
    final SomeDAO dao …
Run Code Online (Sandbox Code Playgroud)

java dependency-injection jdbi dropwizard

9
推荐指数
1
解决办法
3345
查看次数

标签 统计

dependency-injection ×1

dropwizard ×1

java ×1

jdbi ×1