唯一性

于Spring一样,可以为 Bean 指定唯一的名称这就是 ID

  1. package net.test.hasor;
  2. public class HelloModule implements Module {
  3. public void loadModule(ApiBinder apiBinder) throws Throwable {
  4. apiBinder.bindType(InfoBean.class).idWith("beanA");
  5. apiBinder.bindType(InfoBean.class).idWith("beanB");
  6. }
  7. }
  8.  
  9. public class UseBean {
  10. @Inject(value = "beanA" , byType = Type.ByID)
  11. private InfoBean pojoA;
  12. @Inject(value = "beanB" , byType = Type.ByID)
  13. private InfoBean pojoB;
  14. }

也可以通过 AppContext 根据 ID 获取Bean appContext.getInstance("beanA")

同类型的不同Bean

Name的意义是同一个类型的不同 Bean 配置不同的名字,例如:

  1. package net.test.hasor;
  2. public class HelloModule implements Module {
  3. public void loadModule(ApiBinder apiBinder) throws Throwable {
  4. apiBinder.bindType(ICache.class).nameWith("user").to(...);
  5. apiBinder.bindType(ICache.class).nameWith("data").to(...);
  6. }
  7. }
  8.  
  9. public class UseBean {
  10. @Inject("user")
  11. private ICache user;
  12. @Inject("data")
  13. private ICache data;
  14. }