Locked History Actions

Diff for "guice/Manual/Internals/SpringComparison"

Differences between revisions 1 and 2
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
Spring Comparison = Springとの比較 =
Line 3: Line 3:
The Spring Framework, created by Rod Johnson, blazed the Java dependency injection trail. If not the first dependency injection framework, you can certainly credit Spring with pioneering much of what we know about dependency injection and bringing it into the mainstream. Guice might not exist (at least not this early) if not for Spring's example. Rod JohnsonによるSpringフレームワークはJava-DI界で大人気である。
最初のDIフレームワークとは言わないまでも、DIのパイオニアであり、DIをメインフレームにした功労者と言えるだろう。
Springが無ければGuiceも(少なくともこんなに早くには)存在しなかっただろう。
Line 5: Line 7:
Guice and Spring don't compete directly. While Spring is a comprehensive stack, Guice focuses on dependency injection. Even so, the first question most people ask is, "how does Guice compare to Spring?" Rather than repeat the same spiel N times, we figured it best to answer the inevitable question once. GuiceとSpringは直接的には競合しない。
Springは総合的なスタックであり、GuiceはDIに特化している。
そうであっても、人々の最初の質問は「GuiceとSpringの違いは?」である。
何度も熱弁をふるうよりも、この避けがたい質問に一度だけ答えておくのがよいだろう。
Line 7: Line 12:
Let us start by saying, the Guice team has a great deal of respect for the Spring developers, community, and their overall attitude. We're more than willing to work together in any capacity. We even gave a few core Spring developers a sneak peek at Guice six months before the 1.0 release, and they've had access to the source code repository ever since. GuiceチームはSpring開発者やコミュニティ、その姿勢に多大なる尊敬の念を持つことをまず言わせてもらいたい。
我々はいかなる形態でありともに仕事をしたいと思う。
実は1.0のリリースの六ヶ月前に、数人のコアなSpring開発者に内密にソースまで公開したのである。
Line 9: Line 16:
Without further ado, how does Guice compare to Spring? さて、さっさと本題に入ろう。GuiceとSpringがどのように違うのか?
Line 11: Line 18:
Guice is anything but a rehash of Spring. Guice was born purely out of use cases from one of Google's biggest (in every way) applications: AdWords. We sat down and asked ourselves, how do we really want to build this application going forward? Guice enables our answer. GuiceはSpringの焼き直しなどではない。
GuiceはGoogleの(いかなる意味でも)最も大きなアプリケーション「アドワーズ」のユースケースから誕生した。
我々は自問自答した、このアプリを前進させるために、本当に欲しいものは何かと。
Guiceが我々の答えをもたらしたのである。
Line 13: Line 23:
Guice wholly embraces annotations and generics, thereby enabling you to wire together and test objects with measurably less effort. Guice proves you can use annotations instead of error-prone, refactoring-adverse string identifiers. Guiceは全面的にアノテーションとジェネリックスを取り入れている。
それゆえ、オブジェクトをワイアリング、テストするための労力が計れる程度には減少する。
Guiceは、エラーになりがちでリファクタリングに不都合な文字列識別子の代わりにアノテーションを使用できることを証明した。
Line 15: Line 27:
Some new users fear that the number of annotations will get out of hand. Fear not. You won't have nearly as many different annotations in a Guice application as you have string identifiers in a Spring application. 新しいユーザは多くのアノテーションが手に負えなくなることを恐れる場合がある。
大丈夫、Springアプリケーションで使う文字列識別子と同程度のアノテーションにしかGuiceではならない。
Line 17: Line 30:
   1. You only need to use annotations when a binding type alone isn't sufficient.
   2. You can reuse the same annotation type across multiple bindings (@Transactional for example).
   3. Annotations can have attributes with values. You can bind each set of values separately to the same type if you like.
 1. You only need to use annotations when a binding type alone isn't sufficient.
 1. You can reuse the same annotation type across multiple bindings (@Transactional for example).
 1. Annotations can have attributes with values. You can bind each set of values separately to the same type if you like.

Springとの比較

Rod JohnsonによるSpringフレームワークはJava-DI界で大人気である。 最初のDIフレームワークとは言わないまでも、DIのパイオニアであり、DIをメインフレームにした功労者と言えるだろう。 Springが無ければGuiceも(少なくともこんなに早くには)存在しなかっただろう。

GuiceとSpringは直接的には競合しない。 Springは総合的なスタックであり、GuiceはDIに特化している。 そうであっても、人々の最初の質問は「GuiceとSpringの違いは?」である。 何度も熱弁をふるうよりも、この避けがたい質問に一度だけ答えておくのがよいだろう。

GuiceチームはSpring開発者やコミュニティ、その姿勢に多大なる尊敬の念を持つことをまず言わせてもらいたい。 我々はいかなる形態でありともに仕事をしたいと思う。 実は1.0のリリースの六ヶ月前に、数人のコアなSpring開発者に内密にソースまで公開したのである。

さて、さっさと本題に入ろう。GuiceとSpringがどのように違うのか?

GuiceはSpringの焼き直しなどではない。 GuiceはGoogleの(いかなる意味でも)最も大きなアプリケーション「アドワーズ」のユースケースから誕生した。 我々は自問自答した、このアプリを前進させるために、本当に欲しいものは何かと。 Guiceが我々の答えをもたらしたのである。

Guiceは全面的にアノテーションとジェネリックスを取り入れている。 それゆえ、オブジェクトをワイアリング、テストするための労力が計れる程度には減少する。 Guiceは、エラーになりがちでリファクタリングに不都合な文字列識別子の代わりにアノテーションを使用できることを証明した。

新しいユーザは多くのアノテーションが手に負えなくなることを恐れる場合がある。 大丈夫、Springアプリケーションで使う文字列識別子と同程度のアノテーションにしかGuiceではならない。

  1. You only need to use annotations when a binding type alone isn't sufficient.
  2. You can reuse the same annotation type across multiple bindings (@Transactional for example).
  3. Annotations can have attributes with values. You can bind each set of values separately to the same type if you like.

Spring supports two polar configuration styles: explicit configuration and auto-wiring. Explicit configuration is verbose but maintainable. Auto-wiring is concise but slow and not well suited to non-trivial applications. If you have 100s of developers and 100s of thousands of lines of code, auto-wiring isn't an option. Guice uses annotations to support a best-of-both-worlds approach which is concise but explicit (and maintainable).

Some critics have tried to find an analog to Spring's XML (or new Java-based configuration) in Guice. Most times, there isn't any, but sometimes you can't use annotations, and you need to manually wire up an object (a 3rd party component for example). When this comes up, you write a custom provider. A custom provider adds one level of indirection to your configuration and is roughly 1:1 with Spring's XML in complexity. The remaining majority of the time, you'll write significantly less code.

Eric Burke recently ported a Spring application to Guice:

  • At the end of the day, I compared my (Guice) modules — written in Java — to my Spring XML files. The modules are significantly smaller and easier to read. Then I realized about 3/4 of the code in my modules was unnecessary, so I stripped it out. They were reduced to just a few lines of code each.

Oftentimes, it doesn't make sense to port perfectly working code which you won't be changing any time soon to Guice, so Guice works with Spring when possible. You can bind your existing Spring beans. Guice supports AOP Alliance method interceptors which means you can use Spring's popular transaction interceptor.

From our experience, most AOP frameworks are too powerful for most uses and don't carry their own conceptual weight. Guice builds method interception in at the core instead of as an afterthought. This hides the mechanics of AOP from the user and lowers the learning curve. Using one object instead of a separate proxy reduces memory waste and more importantly reduces the chances of invoking a method without interceptors. Under the covers, Guice uses a separate handler for each method minimizing performance overhead. If you intercept just one method in a class, the other methods will not incur overhead.