Java dependency injection just got better: Google Guice 2.0 has been released.
- Provider methods. You can now annotate methods in your
Module so you avoid manually constructing dependencies.
- Binding overrides. One
Module can now override the bindings in another.
- Private modules. You can now create bindings that are not externally visible, in order to encapsulate your dependency injections.
- The ability to inject members of sets and maps using
ServletModule now provides programmatic configuration of servlets and filters.
GuiceServletContextListener can be used to help bootstrap a Guice application in a servlet container.
- Custom injections. Guice now includes hooks that allow other frameworks to define custom injection annotations. This enables Guice to host other frameworks that have their own injection semantics or annotations.
- A tree-like structure for
Injectors, i.e., an
Injector can have children that inherit all of its bindings.
- An introspection API: like reflection but for Guice bindings.
- Pluggable type converters that convert constant string bindings to other types, i.e., Dates, URLs, etc.
- OSGi compatible. Guice now works well in OSGi managed environments, because AOP-generated classes use a bridge classloader.
- AOP is now optional, so Guice will work on platforms that don't support bytecode generation (Android).
You can download the new release here. The new manual is here. Now all they have to do is update the public Maven repository.
On related news (Crazy) Bob Lee, the creator of Guice, and Rod Johnson, the creator of Spring, have gotten together to propose a standard set of annotations and interfaces for using dependency injection in Java under the banner of JSR-330: Dependency Injection for Java (still only a proposal, so JSR-330 is a tentative moniker). There is also a corresponding project over at Google Code. The standardization should greatly help the use of dependency injection in shared 3rd party libraries, allowing the application developer to avoid having to initialize more than one dependency injection framework.
These annotations are modelled closely on those currently in Google Guice:
- @Inject - Identifies injectable constructors, methods, and fields
- @Qualifier - Identifies qualifier annotations
- @Scope - Identifies scope annotations
- @Named - String-based qualifier
- @Singleton - Identifies a type that the injector only instantiates once
The utility interface
Provider is also in the proposed specification.