跳到主要内容

唯一ID

与 Spring一样,可以为 Bean 指定唯一的名称。也就是Bean的 ID。

public class MyModule implements Module {
public void loadModule(ApiBinder apiBinder) throws Throwable {
apiBinder.bindType(InfoBean.class).idWith("beanA");
apiBinder.bindType(InfoBean.class).idWith("beanB");
}
}

获得带名称的 Bean 可以通过 appContext.getInstance("beanA") 或者在依赖注入中指定名字:

public class UseBean {
@Inject(value = "beanA" , byType = Type.ByID)
private InfoBean pojoA;
@Inject(value = "beanB" , byType = Type.ByID)
private InfoBean pojoB;
}