2017-02-28 62 views
1

這是一種奇怪的用例,我需要一些幫助來弄清楚如何協同使用Assisted/Providers/FactoryModuleBuilders。忽略@Singleton的缺席。這只是一個例子。Guice與供應商或FactoryModuleBuilder協作注入

屬於我無法更改的庫的一組特徵具有以下模式。它使用Cake模式。

trait A { //Has abstract methods and abstract service 'webService' } 
trait B extends A { //First Concrete Service Implementation assigned to 'webService' } 
trait C extends A { //Second Concrete service Implementation assigned to 'webService' } 

由於性狀不能直接注射,我創建了一個包裝,使他們被注入

BB extends B 
CC extends C 

在我的代碼,我有依賴於一個服務,這反過來又一個控制器取決於圖書館。該服務應該能夠使用「BB」或「CC」,具體取決於控制器的需求。因此,組件如下所示

創建我的服務爲

//Note: Uses 'trait A' with @Assisted 
class SomeWebServiceComponent @Inject()(@Assisted aInstance: A, foo: Foo){ 
    //Do something with aInstance 
} 

工廠創建這個(應該由吉斯使用FactoryModuleBuilder創建)

class SomeServiceFactory { 
    def getWebServiceComponent(a:A) SomeWebServiceComponent 
} 

的FactoryModule看起來會像這樣

class ApplicationModule extends AbstractModule { 
    override def configure() = { 
    install(new FactoryModuleBuilder().build(classOf[SomeServiceFactory])) 
    } 
} 

我不介意註釋co與我需要的實際課程相結合。

​​

在此設置下,我得到以下那種

No implementation for 'A' annotated with @com.google.inject.assistedinject.Assisted(value=) was bound 

我需要了解我怎麼能告訴吉斯有兩種實現方式的性狀的,即,BB和CC和誤差選擇將在運行時提供。

有沒有辦法實現這個用例?

+1

FactoryModuleBuilder是什麼樣子的? – rethab

+0

@rethab更新了FactoryModuleBuilder用法的問題。 – Serendipity

+0

@rethab您對FactoryModuleBuilder的外觀有什麼疑問,導致我也要檢查Test Base Setup。感謝Duck Coding與我:) – Serendipity

回答

2

好吧,我創建了一個單獨的項目來測試這個整個場景。 它的工作方式是這個問題的框架。

事實證明,我正在使用的測試用例目前並未直接使用GuiceInjection。但是,錯誤消息與GuiceInjection特別相關,我從未調查過測試設置是否正確。

更改測試基準,解決了問題。