On 5/21/07, Bob Lee <[EMAIL PROTECTED]> wrote:
I just wanted to say I'm glad to see you're considering Guice, and I wanted
to address a couple of points in the recent thread.
You have two primary reasons to use Guice:
1) It can make your code more unit testable.
2) You don't have to write boilerplate factory code. For example, you can
just use @Singleton instead of writing a getInstance() method, etc.
I'm a huge fan of immutability. Guice even supports circular references
between constructors so long as you use interfaces. You can literally make
all of your fields final. :)
Guice can indeed invoke non-public constructors, so you can enforce that
singletons really are singletons. It's best to let Guice create your
singleton rather than creating it yourself and passing it to
bind(...).toInstance(...). First, this enables Guice to inject your
singleton's constructor. Second, it enables Guice to optionally lazily load
your singleton. Third, it enables you to use method interception with
singletons. If you want access to your singleton from a classic static
getInstance() method, you can use Guice's static injection support to get
the singleton instance into your class.
Thanks Bob, those three things should be immediately
useful in the work I'm doing with Guice. - Dave