1*3ff81872SXin Li /* 2*3ff81872SXin Li * Copyright (C) 2009 The JSR-330 Expert Group 3*3ff81872SXin Li * 4*3ff81872SXin Li * Licensed under the Apache License, Version 2.0 (the "License"); 5*3ff81872SXin Li * you may not use this file except in compliance with the License. 6*3ff81872SXin Li * You may obtain a copy of the License at 7*3ff81872SXin Li * 8*3ff81872SXin Li * http://www.apache.org/licenses/LICENSE-2.0 9*3ff81872SXin Li * 10*3ff81872SXin Li * Unless required by applicable law or agreed to in writing, software 11*3ff81872SXin Li * distributed under the License is distributed on an "AS IS" BASIS, 12*3ff81872SXin Li * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*3ff81872SXin Li * See the License for the specific language governing permissions and 14*3ff81872SXin Li * limitations under the License. 15*3ff81872SXin Li */ 16*3ff81872SXin Li 17*3ff81872SXin Li /** 18*3ff81872SXin Li * This package specifies a means for obtaining objects in such a way as to 19*3ff81872SXin Li * maximize reusability, testability and maintainability compared to 20*3ff81872SXin Li * traditional approaches such as constructors, factories, and service 21*3ff81872SXin Li * locators (e.g., JNDI). This process, known as <i>dependency 22*3ff81872SXin Li * injection</i>, is beneficial to most nontrivial applications. 23*3ff81872SXin Li * 24*3ff81872SXin Li * <p>Many types depend on other types. For example, a <tt>Stopwatch</tt> might 25*3ff81872SXin Li * depend on a <tt>TimeSource</tt>. The types on which a type depends are 26*3ff81872SXin Li * known as its <i>dependencies</i>. The process of finding an instance of a 27*3ff81872SXin Li * dependency to use at run time is known as <i>resolving</i> the dependency. 28*3ff81872SXin Li * If no such instance can be found, the dependency is said to be 29*3ff81872SXin Li * <i>unsatisfied</i>, and the application is broken. 30*3ff81872SXin Li * 31*3ff81872SXin Li * <p>In the absence of dependency injection, an object can resolve its 32*3ff81872SXin Li * dependencies in a few ways. It can invoke a constructor, hard-wiring an 33*3ff81872SXin Li * object directly to its dependency's implementation and life cycle: 34*3ff81872SXin Li * 35*3ff81872SXin Li * <pre> class Stopwatch { 36*3ff81872SXin Li * final TimeSource timeSource; 37*3ff81872SXin Li * Stopwatch () { 38*3ff81872SXin Li * timeSource = <b>new AtomicClock(...)</b>; 39*3ff81872SXin Li * } 40*3ff81872SXin Li * void start() { ... } 41*3ff81872SXin Li * long stop() { ... } 42*3ff81872SXin Li * }</pre> 43*3ff81872SXin Li * 44*3ff81872SXin Li * <p>If more flexibility is needed, the object can call out to a factory or 45*3ff81872SXin Li * service locator: 46*3ff81872SXin Li * 47*3ff81872SXin Li * <pre> class Stopwatch { 48*3ff81872SXin Li * final TimeSource timeSource; 49*3ff81872SXin Li * Stopwatch () { 50*3ff81872SXin Li * timeSource = <b>DefaultTimeSource.getInstance()</b>; 51*3ff81872SXin Li * } 52*3ff81872SXin Li * void start() { ... } 53*3ff81872SXin Li * long stop() { ... } 54*3ff81872SXin Li * }</pre> 55*3ff81872SXin Li * 56*3ff81872SXin Li * <p>In deciding between these traditional approaches to dependency 57*3ff81872SXin Li * resolution, a programmer must make trade-offs. Constructors are more 58*3ff81872SXin Li * concise but restrictive. Factories decouple the client and implementation 59*3ff81872SXin Li * to some extent but require boilerplate code. Service locators decouple even 60*3ff81872SXin Li * further but reduce compile time type safety. All three approaches inhibit 61*3ff81872SXin Li * unit testing. For example, if the programmer uses a factory, each test 62*3ff81872SXin Li * against code that depends on the factory will have to mock out the factory 63*3ff81872SXin Li * and remember to clean up after itself or else risk side effects: 64*3ff81872SXin Li * 65*3ff81872SXin Li * <pre> void testStopwatch() { 66*3ff81872SXin Li * <b>TimeSource original = DefaultTimeSource.getInstance(); 67*3ff81872SXin Li * DefaultTimeSource.setInstance(new MockTimeSource()); 68*3ff81872SXin Li * try {</b> 69*3ff81872SXin Li * // Now, we can actually test Stopwatch. 70*3ff81872SXin Li * Stopwatch sw = new Stopwatch(); 71*3ff81872SXin Li * ... 72*3ff81872SXin Li * <b>} finally { 73*3ff81872SXin Li * DefaultTimeSource.setInstance(original); 74*3ff81872SXin Li * }</b> 75*3ff81872SXin Li * }</pre> 76*3ff81872SXin Li * 77*3ff81872SXin Li * <p>In practice, supporting this ability to mock out a factory results in 78*3ff81872SXin Li * even more boilerplate code. Tests that mock out and clean up after multiple 79*3ff81872SXin Li * dependencies quickly get out of hand. To make matters worse, a programmer 80*3ff81872SXin Li * must predict accurately how much flexibility will be needed in the future 81*3ff81872SXin Li * or else suffer the consequences. If a programmer initially elects to use a 82*3ff81872SXin Li * constructor but later decides that more flexibility is required, the 83*3ff81872SXin Li * programmer must replace every call to the constructor. If the programmer 84*3ff81872SXin Li * errs on the side of caution and write factories up front, it may result in 85*3ff81872SXin Li * a lot of unnecessary boilerplate code, adding noise, complexity, and 86*3ff81872SXin Li * error-proneness. 87*3ff81872SXin Li * 88*3ff81872SXin Li * <p><i>Dependency injection</i> addresses all of these issues. Instead of 89*3ff81872SXin Li * the programmer calling a constructor or factory, a tool called a 90*3ff81872SXin Li * <i>dependency injector</i> passes dependencies to objects: 91*3ff81872SXin Li * 92*3ff81872SXin Li * <pre> class Stopwatch { 93*3ff81872SXin Li * final TimeSource timeSource; 94*3ff81872SXin Li * <b>@Inject Stopwatch(TimeSource timeSource)</b> { 95*3ff81872SXin Li * this.timeSource = timeSource; 96*3ff81872SXin Li * } 97*3ff81872SXin Li * void start() { ... } 98*3ff81872SXin Li * long stop() { ... } 99*3ff81872SXin Li * }</pre> 100*3ff81872SXin Li * 101*3ff81872SXin Li * <p>The injector further passes dependencies to other dependencies until it 102*3ff81872SXin Li * constructs the entire object graph. For example, suppose the programmer 103*3ff81872SXin Li * asked an injector to create a <tt>StopwatchWidget</tt> instance: 104*3ff81872SXin Li * 105*3ff81872SXin Li * <pre> /** GUI for a Stopwatch */ 106*3ff81872SXin Li * class StopwatchWidget { 107*3ff81872SXin Li * @Inject StopwatchWidget(Stopwatch sw) { ... } 108*3ff81872SXin Li * ... 109*3ff81872SXin Li * }</pre> 110*3ff81872SXin Li * 111*3ff81872SXin Li * <p>The injector might: 112*3ff81872SXin Li * <ol> 113*3ff81872SXin Li * <li>Find a <tt>TimeSource</tt> 114*3ff81872SXin Li * <li>Construct a <tt>Stopwatch</tt> with the <tt>TimeSource</tt> 115*3ff81872SXin Li * <li>Construct a <tt>StopwatchWidget</tt> with the <tt>Stopwatch</tt> 116*3ff81872SXin Li * </ol> 117*3ff81872SXin Li * 118*3ff81872SXin Li * <p>This leaves the programmer's code clean, flexible, and relatively free 119*3ff81872SXin Li * of dependency-related infrastructure. 120*3ff81872SXin Li * 121*3ff81872SXin Li * <p>In unit tests, the programmer can now construct objects directly 122*3ff81872SXin Li * (without an injector) and pass in mock dependencies. The programmer no 123*3ff81872SXin Li * longer needs to set up and tear down factories or service locators in each 124*3ff81872SXin Li * test. This greatly simplifies our unit test: 125*3ff81872SXin Li * 126*3ff81872SXin Li * <pre> void testStopwatch() { 127*3ff81872SXin Li * Stopwatch sw = new Stopwatch(new MockTimeSource()); 128*3ff81872SXin Li * ... 129*3ff81872SXin Li * }</pre> 130*3ff81872SXin Li * 131*3ff81872SXin Li * <p>The total decrease in unit-test complexity is proportional to the 132*3ff81872SXin Li * product of the number of unit tests and the number of dependencies. 133*3ff81872SXin Li * 134*3ff81872SXin Li * <p><b>This package provides dependency injection annotations that enable 135*3ff81872SXin Li * portable classes</b>, but it leaves external dependency configuration up to 136*3ff81872SXin Li * the injector implementation. Programmers annotate constructors, methods, 137*3ff81872SXin Li * and fields to advertise their injectability (constructor injection is 138*3ff81872SXin Li * demonstrated in the examples above). A dependency injector identifies a 139*3ff81872SXin Li * class's dependencies by inspecting these annotations, and injects the 140*3ff81872SXin Li * dependencies at run time. Moreover, the injector can verify that all 141*3ff81872SXin Li * dependencies have been satisfied at <i>build time</i>. A service locator, 142*3ff81872SXin Li * by contrast, cannot detect unsatisfied dependencies until run time. 143*3ff81872SXin Li * 144*3ff81872SXin Li * <p>Injector implementations can take many forms. An injector could 145*3ff81872SXin Li * configure itself using XML, annotations, a DSL (domain-specific language), 146*3ff81872SXin Li * or even plain Java code. An injector could rely on reflection or code 147*3ff81872SXin Li * generation. An injector that uses compile-time code generation may not even 148*3ff81872SXin Li * have its own run time representation. Other injectors may not be able to 149*3ff81872SXin Li * generate code at all, neither at compile nor run time. A "container", for 150*3ff81872SXin Li * some definition, can be an injector, but this package specification aims to 151*3ff81872SXin Li * minimize restrictions on injector implementations. 152*3ff81872SXin Li * 153*3ff81872SXin Li * @see javax.inject.Inject @Inject 154*3ff81872SXin Li */ 155*3ff81872SXin Li package javax.inject; 156