【问题标题】:Guice Injection: parameter not explicity boundGuice 注入:参数未明确绑定
【发布时间】:2017-08-22 00:54:48
【问题描述】:

我正在尝试将一些代码转换为新格式,并且遇到了 dropwizard 资源的问题。这是我添加的资源类注入

@Inject
public TestResource(TestDAO testDAO, String path) {

TestDAO 是一个扩展 io.dropwizard.hibernate.AbstractDAO 的类。当我尝试通过注入运行它时,我收到了

1) Explicit bindings are required and ...TestDAO is not explicitly bound.
  while locating ...TestDAO
    for the 1st parameter of TestResource.<init>(TestResource.java:44)

2) Explicit bindings are required and java.lang.String is not explicitly bound.
  while locating java.lang.String
    for the 2nd parameter of ...TestResource.<init>(TestResource.java:44)

【问题讨论】:

  • 你认为你在哪里明确绑定了这些参数
  • 我从来没有说过我以为我是。我不知道我需要为显式绑定做什么。我是 Java 新手。我查看了使用它的其他项目和文档,但没有看到我应该添加什么。

标签: java guice code-injection


【解决方案1】:

在此模板中生成了一个扩展 AbstractModule 的类。在那里有一个对 configure() 的覆盖,它具有绑定。

bind(TestResource.class).in(Singleton.class);

我还需要修改我的 DAO 类,以便它们可以支持注入。

【讨论】:

    【解决方案2】:

    根本原因在这里得到了很好的解释When to provide explicit binding and when to leverage the automatic binding on Guice

    但对我来说,这是因为并非所有战争都已部署。在 Eclipse 的 Tomcat Add and Remove... 界面中,我未能包含我想要启动的应用程序模块中的所有战争。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-16
      • 1970-01-01
      • 2018-09-21
      • 2018-04-25
      • 2019-06-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多