2016-04-27 50 views
0

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

有人可以幫助我,我做錯了什麼?

+0

你有沒有綁定MyServiceConfig地方?你使用的是什麼樣的Guice集成? – pandaadb

回答

0

我懷疑你的問題是dropwizard在引擎蓋下使用了Jersey。注意這個例外不是關於Guice,而是Jersey。澤西和吉斯需要一些調整一起工作。澤西島有自己的DI框架(HK2)。你需要橋接它們。

編輯:看看dropwizard-guicey,一個針對您的問題的項目。感謝下面的評論@pandaadb。

非常快的代碼示例:

@ApplicationPath("api") 
public class ApiRest extends ResourceConfig { 
    private static final Logger log = LoggerFactory.getLogger(ApiRest.class); 

    @Inject 
    public ApiRest(ServiceLocator serviceLocator, ServletContext servletContext) { 
     log.debug("Inicialitzant Jersey."); 
     packages("net.sargue.app.api"); 

     GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator); 
     GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class); 
     Injector injector = (Injector) servletContext.getAttribute(Injector.class.getName()); 
     if (injector == null) 
      throw new RuntimeException("Guice Injector not found"); 
     guiceBridge.bridgeGuiceInjector(injector); 
    } 
} 

,會爲一個標準的新澤西/ JAX-RS API工作。你可能需要調整到dropwizard。

看到這個問題,我的回答:

+0

在這種情況下,有集成框架爲您處理。我使用https://github.com/xvik/dropwizard-guicey,我對此非常滿意。這會自動發現並注入所有運動衫組件(提供者,過濾器,資源等)。 – pandaadb