我正在研究一個需要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