Akashic Records

Spring All Dependency Injection Types 본문

Spring.io

Spring All Dependency Injection Types

Andrew's Akashic Records 2021. 2. 19. 17:42
728x90

Constructor Injection —enforcing immutability

This is the most straightforward and recommended way of dependency injection. A dependent class has a constructor, where all dependencies are set, they will be provided by Spring container according to XML, Java or annotation based configurations.

@Service
public class DependentService {
    private final Service1 service1;
    private final Service2 service2;
    @Autowired
    public DependentService(Service1 service1, Service2 service2) {
        this.service1 = service1;
        this.service2 = service2;
    }
    void doSmth() {
        service1.doSmth();
        service2.doSmth();
    }
}

Advantages

  1. Constructed object is immutable and returned to the client in a fully initialized state.
  2. An issue with a growing amount of dependencies is immediately visible with this approach. More dependencies bigger the constructor.
  3. Can be combined with setter injection or field injection, constructor parameters indicate required dependencies, others — optional.

Disadvantages

  1. No possibility to change object’s dependencies later — inflexibility.
  2. Higher chance to have circular dependencies, so-called chicken-and-egg scenario.

Setter injection — enjoy the mutability

@Service
public class DependentService {
    private Service1 service1;
    private Service2 service2;
    @Autowired
    public void setService1(Service1 service1) {
        this.service1 = service1;
    }
    @Autowired
    public void setService2(Service2 service2) {
        this.service2 = service2;
    }
    void doSmth() {
        service1.doSmth();
        service2.doSmth();
    }
}

Advantages

  1. Flexibility in dependency resolution or object reconfiguration, it can be done anytime. Plus, this freedom solves the circular dependency issue of constructor injection.

Disadvantages

  1. Null checks are required, because dependencies may not be set at the moment.
  2. Potentially more error-prone and less secure than constructor injection due to the possibility of overriding dependencies.

Field injection — nobody likes it?

Advantages

  1. Easy to use, no constructors or setters required
  2. Can be easily combined with the constructor and/or setter approach

Disadvantages

  1. Less control over object instantiation. In order to instantiate the object of a class for a test, you will need either a Spring container configured or mock library — depends on the test you are writing.
  2. A number of dependencies can reach dozens until you notice that something went wrong in your design.
  3. No immutability — the same as for setter injection.

Conclusion

  1. Constructor injection — good, reliable and immutable, inject via one of the constructors. Possible to configure in: XML, XML+Annotations, Java, Java + Annotations.
  2. Setter injection — more flexible, mutable objects, injection via setters. Possible to configure in: XML, XML+Annotations, Java, Java + Annotations.
  3. Field injection — fast and convenient, coupling with IoC container. Possible to configure in XML+Annotations, Java + Annotations.

 

This article is excerpted here.

728x90

'Spring.io' 카테고리의 다른 글

Spring Batch+quartz 연동  (0) 2023.03.31
Spring Boot Actuator  (0) 2023.03.23
Spring Boot Project 만들기  (0) 2021.01.26
Spring batch - ItemReaders, ItemWriters and ItemStream  (0) 2020.12.21
Spring Boot file uploader  (0) 2020.11.26
Comments