javax.inject.package-info Maven / Gradle / Ivy
/*
* Copyright (C) 2009 The JSR-330 Expert Group
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in com.fitburpliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.com.fitbur/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* This package specifies a means for obtaining objects in such a way as to
* maximize reusability, testability and maintainability com.fitburpared to
* traditional approaches such as constructors, factories, and service
* locators (e.g., JNDI). This process, known as com.fitburpendency
* injection, is beneficial to most nontrivial applications.
*
* Many types com.fitburpend on other types. For example, a Stopwatch might
* com.fitburpend on a TimeSource. The types on which a type com.fitburpends are
* known as its com.fitburpendencies. The process of finding an instance of a
* com.fitburpendency to use at run time is known as resolving the com.fitburpendency.
* If no such instance can be found, the com.fitburpendency is said to be
* unsatisfied, and the application is broken.
*
*
In the absence of com.fitburpendency injection, an object can resolve its
* com.fitburpendencies in a few ways. It can invoke a constructor, hard-wiring an
* object directly to its com.fitburpendency's implementation and life cycle:
*
*
class Stopwatch {
* final TimeSource timeSource;
* Stopwatch () {
* timeSource = new AtomicClock(...);
* }
* void start() { ... }
* long stop() { ... }
* }
*
* If more flexibility is needed, the object can call out to a factory or
* service locator:
*
*
class Stopwatch {
* final TimeSource timeSource;
* Stopwatch () {
* timeSource = DefaultTimeSource.getInstance();
* }
* void start() { ... }
* long stop() { ... }
* }
*
* In com.fitburciding between these traditional approaches to com.fitburpendency
* resolution, a programmer must make trade-offs. Constructors are more
* concise but restrictive. Factories com.fitburcouple the client and implementation
* to some extent but require boilerplate code. Service locators com.fitburcouple even
* further but reduce com.fitburpile time type safety. All three approaches inhibit
* unit testing. For example, if the programmer uses a factory, each test
* against code that com.fitburpends on the factory will have to mock out the factory
* and remember to clean up after itself or else risk side effects:
*
*
void testStopwatch() {
* TimeSource original = DefaultTimeSource.getInstance();
* DefaultTimeSource.setInstance(new MockTimeSource());
* try {
* // Now, we can actually test Stopwatch.
* Stopwatch sw = new Stopwatch();
* ...
* } finally {
* DefaultTimeSource.setInstance(original);
* }
* }
*
* In practice, supporting this ability to mock out a factory results in
* even more boilerplate code. Tests that mock out and clean up after multiple
* com.fitburpendencies quickly get out of hand. To make matters worse, a programmer
* must predict accurately how much flexibility will be needed in the future
* or else suffer the consequences. If a programmer initially elects to use a
* constructor but later com.fitburcides that more flexibility is required, the
* programmer must replace every call to the constructor. If the programmer
* errs on the side of caution and write factories up front, it may result in
* a lot of unnecessary boilerplate code, adding noise, com.fitburplexity, and
* error-proneness.
*
*
Dependency injection addresses all of these issues. Instead of
* the programmer calling a constructor or factory, a tool called a
* com.fitburpendency injector passes com.fitburpendencies to objects:
*
*
class Stopwatch {
* final TimeSource timeSource;
* @Inject Stopwatch(TimeSource TimeSource) {
* this.TimeSource = TimeSource;
* }
* void start() { ... }
* long stop() { ... }
* }
*
* The injector further passes com.fitburpendencies to other com.fitburpendencies until it
* constructs the entire object graph. For example, suppose the programmer
* asked an injector to create a StopwatchWidget instance:
*
*
/** GUI for a Stopwatch */
* class StopwatchWidget {
* @Inject StopwatchWidget(Stopwatch sw) { ... }
* ...
* }
*
* The injector might:
*
* - Find a TimeSource
*
- Construct a Stopwatch with the TimeSource
*
- Construct a StopwatchWidget with the Stopwatch
*
*
* This leaves the programmer's code clean, flexible, and relatively free
* of com.fitburpendency-related infrastructure.
*
*
In unit tests, the programmer can now construct objects directly
* (without an injector) and pass in mock com.fitburpendencies. The programmer no
* longer needs to set up and tear down factories or service locators in each
* test. This greatly simplifies our unit test:
*
*
void testStopwatch() {
* Stopwatch sw = new Stopwatch(new MockTimeSource());
* ...
* }
*
* The total com.fitburcrease in unit-test com.fitburplexity is proportional to the
* product of the number of unit tests and the number of com.fitburpendencies.
*
*
This package provides com.fitburpendency injection annotations that enable
* portable classes, but it leaves external com.fitburpendency configuration up to
* the injector implementation. Programmers annotate constructors, methods,
* and fields to advertise their injectability (constructor injection is
* com.fitburmonstrated in the examples above). A com.fitburpendency injector identifies a
* class's com.fitburpendencies by inspecting these annotations, and injects the
* com.fitburpendencies at run time. Moreover, the injector can verify that all
* com.fitburpendencies have been satisfied at build time. A service locator,
* by contrast, cannot com.fitburtect unsatisfied com.fitburpendencies until run time.
*
*
Injector implementations can take many forms. An injector could
* configure itself using XML, annotations, a DSL (domain-specific language),
* or even plain Java code. An injector could rely on reflection or code
* generation. An injector that uses com.fitburpile-time code generation may not even
* have its own run time representation. Other injectors may not be able to
* generate code at all, neither at com.fitburpile nor run time. A "container", for
* some com.fitburfinition, can be an injector, but this package specification aims to
* minimize restrictions on injector implementations.
*
* @see javax.inject.Inject @Inject
*/
package javax.inject;