当您有Spring和Dagger时,为什么要使用/开发Guice? [英] Why use/develop Guice, when You have Spring and Dagger?

查看:491
本文介绍了当您有Spring和Dagger时,为什么要使用/开发Guice?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,Dagger确实会生成代码,而Guice和Spring依赖于运行时处理,因此Dagger的运行速度更快,但需要在程序员方面进行更多工作.由于性能优势,它非常适合移动(Android)开发.

To my knowledge, Dagger does generate code, while Guice and Spring rely on runtime processing, thus Dagger works faster, but requires more work on programmer side. Because of performance edge it's good for mobile (Android) development.

但是,当我们剩下Guice和Spring时,后者具有很多集成.如果我们可以使用Spring框架(这基本上是一样的,但是可以提供更容易的数据库访问),那么开发/使用Guice的意义何在?

However, when we are left with Guice and Spring, the latter has lots of integrations. What's the point of developing/using Guice, if we can use Spring Framework (that does basically same thing, but offers ex. easier database access)?

Google是否不是通过创建自己的DI工具而不是使用Spring Framework(并可能对Spring Framework做出贡献)来重塑轮子?

Isn't Google trying to reinvent wheel by creating their own DI tool, instead of using (and possibly contributing to) Spring Framework?

我正在寻找决策树,以指导您选择DI工具.

I am looking for decision tree, that guides through choosing DI tool.

推荐答案

重要的是要认识到Dagger是Guice的一位创建者在Guice之后创建的(已经搬到Square的疯狂鲍勃"李):

It's important to realize that Dagger was created after Guice, by one of Guice's creators ("Crazy Bob" Lee) who had moved to Square:

  • Spring was originally released in October 2002.
  • Google originally released Guice publicly in March 2007.
  • JSR-330 formalized javax.inject annotations in October 2009, with heavy input from Google (Bob Lee), Spring, and other industry players.
  • Square originally released Dagger 1 publicly in May 2013.
  • Google originally released Dagger 2 publicly in April 2015.
  • Square marked Dagger 1 as deprecated 10 days before this question was asked, on September 15, 2016.

从这个意义上讲,Guice的持续策划并不是在重新发明轮子",而只是对长期运行且被广泛使用的软件包进行维护,该软件包彻底早于Dagger的任何版本.您可能会认为Dagger是Guice的精神继任者,但Dagger只提供了Guice功能的优化子集.

In that sense, the continued curation of Guice isn't "reinventing the wheel" so much as maintenance on a long-running and widely-consumed software package that thoroughly predates any version of Dagger. You might consider Dagger to be a spiritual successor to Guice, but one that only offers an optimized subset of Guice's functionality.

要列出并修改您上面的区别:

To list and amend to the differences you have above:

  • Spring是一个相对较重的框架,具有许多集成,一种XML配置语言以及运行时/反射性绑定.已经使用Spring的应用程序可以使用Spring的依赖项注入框架,而无需进行额外的工作.
  • Guice是一个相对轻量级的框架,具有较少的集成,Java实例配置以及运行时/反射绑定.通过使用Java绑定,您可以获得编译时类型检查和IDE自动完成集成.
  • Dagger是一个非常轻量级的框架,具有很少的集成,Java接口/注释配置以及编译时代码生成的绑定.代码生成方面使Dagger总体上表现出色,尤其是在资源有限和移动环境中. (Android的VM与服务器JRE的不同之处在于,它们使反射特别慢,因此Dagger在这里特别有用.)
  • 以上三个框架均支持JSR-330,因此,精心设计的库或应用程序几乎与所使用的DI容器无关.

除此之外,请密切注意所使用的任何框架中的维护/弃用模式和策略.根据您团队的知识和经验,对反射或运行时配置的需求以及对集成和运行时性能的需求,您可能会发现上述突出之一.也就是说,还有其他框架,因此请密切注意上述新选项和分支.

Beyond that, keep an eye out for maintenance/deprecation patterns and policies among any framework you use. Based on your team's knowledge and experience, your need for reflection or runtime configuration, and your need for integrations and runtime performance, you'll probably see one of the above stand out. That said, there are also other frameworks out there, so keep an eye open for new options and forks of the above as well.

这篇关于当您有Spring和Dagger时,为什么要使用/开发Guice?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆