【发布时间】:2011-03-17 02:09:26
【问题描述】:
我有这门课:
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;
}
我的问题嵌入在代码 cmets 中。当我将StoreAuthorizer 添加到List<SecurityAuthorizer> 时:
- 我如何确保它与其他
StoreAuthorizer引用的实例相同? - Guice 只是在幕后做的事情,所以
new StoreAuthorizer()真的在幕后调用getInstance()的 impl 吗?
【问题讨论】:
标签: dependency-injection singleton guice