我正在研究一个需要Guice与dropwizard集成的项目。以下是我设计它的方式。Dropwizard Guice错误:缺少对构造函数的依赖
ApplicationClass
public class MyService extends Application<MyServiceConfig> {
public static void main(String[] args) throws Exception {
new MyService().run(args);
}
@Override
public void initialize(Bootstrap<MyServiceConfig> bootstrap) {
GuiceBundle<MyServiceConfig> guiceBundle = GuiceBundle.<MyServiceConfig>newBuilder()
.addModule(new MyServiceModule())
.setConfigClass(MyServiceConfig.class)
.enableAutoConfig(this.getClass().getPackage().getName())
.build();
bootstrap.addBundle(guiceBundle);
}
@Override
public void run(MyServiceConfig config, Environment environment) throws Exception{
}
这是我写的
public class MyServiceModule extends AbstractModule{
@Provides
public MyDataStoreInterface getDataStore(MyServiceConfig myServiceConfig){
return new MyDataStore(myServiceConfig.getConfig1(), myServiceConfig.getConfig2());
}
@Override
protected void configure() {
}
}
这是我的数据存储文件
public interface MyDataStoreInterface {
public void method1();
}
public class MyDataStore implements MyDataStoreInterface {
public MyDataStore(Config1 config1, Config2 config2){
/*implementation*/
}
public void method1(){
/*implementation*/
}
}
而且最后以下是我的资源文件MyServiceModule
@Path("/documents")
@Produces(MediaType.APPLICATION_JSON)
public class MyResource {
private MyDataStoreInterface myDataStore;
@Inject
public MyResource(MyDataStoreInterface myDataStore) {
this.myDataStore = myDataStore;
}
}
当我运行上面的应用程序,我得到以下错误:
ERROR [2016-04-27 20:34:29,669] com.sun.jersey.spi.inject.Errors: The following errors and warnings have been detected with resource and/or provider classes:
SEVERE: Missing dependency for constructor public com.mobile.myservice.resources.MyResource(com.mobile.myservice.datastore.MyDataStoreInterface) at parameter index 0
有人可以帮助我,我做错了什么?
你有没有绑定MyServiceConfig地方?你使用的是什么样的Guice集成? – pandaadb