Home > Java > javaTutorial > Why Is Constructor or Setter Injection Preferred Over Field Injection in Spring?

Why Is Constructor or Setter Injection Preferred Over Field Injection in Spring?

DDD
Release: 2024-12-08 01:54:12
Original
283 people have browsed it

Why Is Constructor or Setter Injection Preferred Over Field Injection in Spring?

What is Field Injection and Why it's Generally Not Preferred

In Spring MVC and Portlet applications, field injection refers to the practice of directly injecting a dependency into a field annotated with @Autowired, as demonstrated in the following example:

@Component
public class MyComponent {
    @Autowired
    private Cart cart;
}
Copy after login

As an alternative, constructor injection involves passing the dependency as a parameter in the constructor:

@Component
public class MyComponent {
    private final Cart cart;

    @Autowired
    public MyComponent(Cart cart) {
        this.cart = cart;
    }
}
Copy after login

Advantages and Disadvantages:

Constructor injection offers several advantages:

  • Immutability: It allows for creating immutable objects, as the dependencies are assigned upon instantiation.
  • Loose coupling: Classes have a looser dependency on the DI container and can be used outside of it.
  • Testability: Objects can be instantiated for unit testing without the need for reflection.

Setter injection is also an option, providing a mechanism for injecting dependencies that may be optional or changeable over time.

In contrast, field injection suffers from several drawbacks:

  • Tight coupling: It creates a tight coupling with the DI container.
  • Immutability: It hinders the creation of immutable objects.
  • Reflection requirement: It requires reflection for instantiation outside the DI container.
  • Hidden dependencies: Real dependencies are obscured from the interface.
  • Too many dependencies: It encourages the addition of excessive dependencies, violating the Single Responsibility Principle.

Injection Guidelines:

Spring recommends the following guidelines:

  • Use constructor injection for mandatory dependencies or for achieving immutability.
  • Use setter injection for optional or changeable dependencies.
  • Avoid field injection in most cases.

Conclusion:

Considering the disadvantages of field injection, it is generally advisable to favor constructor or setter injection. The convenience of field injection is outweighed by its potential drawbacks and limitations.

The above is the detailed content of Why Is Constructor or Setter Injection Preferred Over Field Injection in Spring?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template