5
在實例我有這個類:獲得一個吉斯模塊
public class CompositeSecurityAuthorizer implements SecurityAuthorizer {
@inject @CompositeSecurityAuthorizerAnnot
List<SecurityAuthorizer> authorizers; //Field Injection
}
我想注入authorizers
領域List<SecurityAuthorizer>
值。
在我的模塊,我有以下幾點:
@Override
protected void configure() {
bind(CompositeSecurityAuthorizer.class).in(Singleton.class);
bind(StoreAuthorizer.class).in(Singleton.class);
bind(SecurityAuthorizer.class)
.annotatedWith(CompositeSecurityAuthorizerAnnot.class)
.to(CompositeSecurityAuthorizer.class);
}
@Provides @CompositeSecurityAuthorizerAnnot
List<SecurityAuthorizer> provideAuthorizersList()
{
List<SecurityAuthorizer> authList = new ArrayList<SecurityAuthorizer>();
//How do I add StoreAuthorizer while maintaining a Singleton?
//Will the line below do it through Guice magic?
//authList.add(new StoreAuthorizer());
return authList;
}
我的問題是嵌入代碼中的註釋。當我加入StoreAuthorizer
到List<SecurityAuthorizer>
:
- 我如何確保它是相同的實例作爲其他
StoreAuthorizer
參考? - Guice只是在引擎蓋下做的事情,所以
new StoreAuthorizer()
真的在幕後調用了getInstance()
的impl嗎?
我忘記了MultiBinder。所以像這樣? Multibinder securityBinder = Multibinder.newSetBinder(binder(),SecurityAuthorizer.class); securityBinder.addBinding()。(StoreAuthorizer.class); –
Snekse
2011-03-16 19:24:34
@Snekse:是的,類似的東西。 – ColinD 2011-03-16 19:30:30
Opps,看起來像Multibinder不兼容杜松子酒。 http://code.google.com/p/google-gin/issues/detail?id=111 – Snekse 2011-03-16 19:32:22