Android 和 Guice - 如何传递参数?

发布于 2024-10-28 17:59:54 字数 354 浏览 4 评论 0原文

我希望能够注入一个对象,并将参数传递给其初始化方法。 这可能吗?

public class MyObject
{
  @Inject
  public MyObject(int anInteger)
  {
    //do something
  }
}


public class MyActivity extends RoboActivity
{
   @Inject (anInteger = 5) MyObject myObject;
   // I want to be able to pass an object to be used when calling the 
   // initializer method
}

I want to be able to inject an object, and pass a parameter to its initializer method.
Is this possible?

public class MyObject
{
  @Inject
  public MyObject(int anInteger)
  {
    //do something
  }
}


public class MyActivity extends RoboActivity
{
   @Inject (anInteger = 5) MyObject myObject;
   // I want to be able to pass an object to be used when calling the 
   // initializer method
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌无敌 2024-11-04 17:59:54

您应该能够使用 bindConstant() 来完成此操作并相应地对其进行注释。例如,请参阅如何注入配置参数?

You should be able to do it with bindConstant() and annotating it accordingly. See, for example, How do I inject configuration parameters?

淡淡的优雅 2024-11-04 17:59:54
public class MyModule extends AbstractModule
{
  @Override
  protected void configure()
  {
    bind(Integer.class).
        annotatedWith(Names.named("my.object.an.integer")).
        toInstance(500);
  }

  @Provides
  @Named("an.integer.5")
  public MyObject myObject5()
  {
    return createMyObject(5);
  }

  @Provides
  @Named("an.integer.100")
  public MyObject providesMyObject100()
  {
    return createMyObject(100);
  }

  private MyObject createMyObject(int anInteger)
  {
    MyObject result = new MyObject(anInteger);
    // if there are any other fields/setters annotated with @Inject
    requestInjection(result);
    return result;
  }
}


public class MyObject
{
  public MyObject(int anInteger)
  {
    System.out.println("anInteger = " + anInteger);
  }
}

public class User
{
  @Inject
  @Named("an.integer.5")
  private field MyObject five;

  @Inject
  @Named("an.integer.100")
  private field MyObject hundred;
}
public class MyModule extends AbstractModule
{
  @Override
  protected void configure()
  {
    bind(Integer.class).
        annotatedWith(Names.named("my.object.an.integer")).
        toInstance(500);
  }

  @Provides
  @Named("an.integer.5")
  public MyObject myObject5()
  {
    return createMyObject(5);
  }

  @Provides
  @Named("an.integer.100")
  public MyObject providesMyObject100()
  {
    return createMyObject(100);
  }

  private MyObject createMyObject(int anInteger)
  {
    MyObject result = new MyObject(anInteger);
    // if there are any other fields/setters annotated with @Inject
    requestInjection(result);
    return result;
  }
}


public class MyObject
{
  public MyObject(int anInteger)
  {
    System.out.println("anInteger = " + anInteger);
  }
}

public class User
{
  @Inject
  @Named("an.integer.5")
  private field MyObject five;

  @Inject
  @Named("an.integer.100")
  private field MyObject hundred;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文