I. Introduction
When we use the Spring framework for the development, it is inevitable to dependency injection (Dependency Injection), is removed from the instance Spring container for use. Spring dependency There are three main injection, respectively Constructor, Setter, and Field. With room for choice, tangled place to come, these three methods which better?
Second, the injection method comparison
Constructor Injection
private DependencyA dependencyA; @Autowired public DI(DependencyA dependencyA) { this.dependencyA = dependencyA; }
Setter Injection
private DependencyB dependencyB; @Autowired public void setDependencyB(DependencyB dependencyB) { this.dependencyB = dependencyB; }
Field injection
@Autowired private DependencyC dependencyC;
Third, the choice of which injection method
Comparison of these three ways, Field inject looked fresh and clean. Fast and convenient time to write, read and very pleasing, it looks set to give priority to choose to use it ah. However, this approach is there are some drawbacks.
- As simple, concise code, the user may inject too much too easy to rely on, resulting in the class took on too much responsibility. Contrary to the design principle of single responsibility. As to why to follow the principle of a single, self-Baidu.
- Since no full parameter set configuration or method, then the user is possible by the new default configuration of an example null reference, the use of a null pointer exception occurs.
- Such can not be outside the DI container (test, other modules) reuse because in addition to the reflection, there is no other way to provide the required dependency.
- It is possible to produce circular dependencies reported BeanCurrentlyInCreationException, below is just one example, the case will appear in reality more formidable.
public class A { @Autowired private B b; } public class B { @Autowired private C c; } public class C { @Autowired private A a; }
Spring officially is currently recommended constructor injection. According to the official statement, because it will enable people to achieve application components as immutable objects , and to ensure that the required dependency is not null . In addition, the injection assembly constructor always fully initialized state is returned to the client (calling) code.
-
Immutable objects: that is the field with the final keyword can be modified.
-
Depend not null: Because of the self-defined constructor, the program no longer provides default empty configuration parameters, must pass all the necessary parameters based upon instantiation.
-
Fully initialized state: the role of the constructor is to initialize the member variables in Java class loading process instantiation, the constructor is the last step, the components are returned to the state after initialization.
to sum up
Field injection
advantage
-
The most succinct
Shortcoming
-
Convenience will weaken the structural design codes
-
-
Is not dependent variable (not final)
-
Prone to circular dependencies
-
You need to use multiple spring or java annotations
Set injection
advantage
- Circulating immune dependent
- With the addition of a setter, highly coupled classes are easily identified.
Shortcoming
- Violation of the open closed principle
- Cycle will depend hide
- Three methods most templated way
- Is not dependent variable (not final)
Constructor Injection
advantage
-
It may be dependent on the final
-
There are three ways the easiest way to test
-
With the growth of high coupling type configuration parameters are easily identified
-
Other development platform developers are very familiar
-
It does not depend on @Autowired comment
Shortcoming
- The constructor needs to settle into subclasses