【发布时间】:2016-08-22 10:39:49
【问题描述】:
我正在开发一个需要 Guice 与 dropwizard 集成的项目。以下是我设计的方式。
应用类
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{
}
这是我写的 MyServiceModule
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*/
}
}
最后是我的资源文件
@Path("/documents")
@Produces(MediaType.APPLICATION_JSON)
public class MyResource {
private MyDataStoreInterface myDataStore;
@Inject
public MyResource(MyDataStoreInterface myDataStore) {
this.myDataStore = myDataStore;
}
}
当我运行上述应用程序时,我收到以下错误:
错误 [2016-04-27 20:34:29,669] com.sun.jersey.spi.inject.Errors: 使用资源和/或检测到以下错误和警告 提供者类:
严重:缺少构造函数公共的依赖项 com.mobile.myservice.resources.MyResource(com.mobile.myservice.datastore.MyDataStoreInterface) 在参数索引 0
有人可以帮我解决我做错的地方吗?
【问题讨论】:
-
你有没有在任何地方绑定过 MyServiceConfig?您使用的是哪种 Guice 集成?
标签: java guice jersey-2.0 dropwizard