java - 我应该使用 RoboGuice 还是其他依赖注入(inject)框架?

标签 java android dependency-injection roboguice

我从一位 friend 那里听说,使用 RoboGuice 使 Coding 变得更加容易和简单。 。但我发现Official document of Android当我读this时。作者在第二点中说道:

Official document of Android suggests to avoid dependency injection frameworks like Guice or RoboGuice, reason being is it’s a reflection based library and it creates overheads at run time. Even it tends to perform a lot of process initialization by scanning your code for annotations, which can require significant amounts of your code to be mapped into RAM even though you don’t need it. (Here I would say we can use Android annotation library because it creates duplicate copy of annotated classes so it won’t create any overhead at runtime but at compile time.)

那么我应该使用它吗?任何帮助将不胜感激。

最佳答案

是的,它变得越来越容易,但是正如您在官方文档中所读到的,就像您上面所说的那样,这种依赖注入(inject)会降低您的应用程序性能,因为它依赖于 Java's Reflection .
但并非所有依赖注入(inject)都使用反射,如 ButterknifeDagger 2因此它不会影响您的应用性能。

Butterknife 用法示例:

class ButterknifeActivity extends Activity {
  @Bind(R.id.title) TextView title;
  @Bind(R.id.logo) ImageView logo;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    ButterKnife.bind(this);
  }
}

使用 Butterknife,您不需要调用 findViewById 并强制转换它,而是库会为您执行此操作。

关于java - 我应该使用 RoboGuice 还是其他依赖注入(inject)框架?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31552377/

相关文章:

c# - 在 ASP .NET Core Web API Controller 中注入(inject) Serilog 的 ILogger 接口(interface)

java - 将 Jetty 线程池的一部分专用于特定端点

java - 无法使用 jersey-quickstart-webapp 原型(prototype)创建新的 Maven 项目

java - Android - 检测流结束而不是 MediaPlayer 中的连接错误

java - 将 GUI 链接到主类

android - TextView:用多行包裹内容

php - 如何在 Symfony 4 的控制台命令中使用记录器?

android - 错误 :Execution failed for task ':app:dexDebug' when importing ParseUI-widget and Login as modules

android - 计费库 2.0 中的新 "Resubscribe"功能是什么?它与旧的计费库 1.2.2 有何不同?

scala - Scaldi 和 Guice 和有什么不一样