Guice Injection:参数未明确约束

时间:2017-08-22 00:54:49

标签: java guice code-injection

我正在尝试将一些代码转换为新格式,并且遇到了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)

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

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

然而对我而言,并非所有战争都得到了部署。在Eclipse的Tomcat 添加和删除... 界面中,我未能包含我想要启动的所有应用程序模块的战争。