Dependency Injection Annotations To Come To A JVM Near You.
(Via Twitter, where else?)
Remember this (my blog reaction to Google Guice 1.0)? At the time I wondered:
Me:Guice, Or Something Like It, Should Be In The JDK
About the only thing that makes feel a little bit uncomfortable is the use of Guice specific imports in my application code.
Wouldn't it be nice if I don't have to do a
import com.google.inject.*;but do aimport java.lang.inject.*;instead?
It's been 779 days since then, but Bob Lee and Rod Johnson have just made public a (work-in-progress) JSR JSR ?: Annotations for Dependency Injection "to standardize a proven, non-controversial set of annotations that make injectable classes portable across frameworks."
Bob Lee: Five years ago, Spring 1.0 brought Java dependency injection into the mainstream. Three years later, Google Guice 1.0 introduced annotation-based dependency injection and made Java programming a little easier. Since then, developers have had to choose between a) writing external configuration or b) importing vendor-specific annotations.
Today, we hope to give developers the best of both worlds. Google Guice and SpringSource have partnered to standardize a proven, non-controversial set of annotations that make injectable classes portable across frameworks. At the moment, the set of specified annotations consists of:
- @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
One additional interface is specified for use in conjunction with these annotations:
- Provider<T> - Provides instances of a type T. For any type T that can be injected, you can also inject Provider<T>.