PageRenderTime 27ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/src/javax/inject/package-info.java

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