Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- oracle
- android
- 웹 크롤링
- 리눅스
- 자바네트워크
- 시스템
- 소프트웨어공학
- Spring boot
- Database
- kotlin
- 유닉스
- 코틀린
- NIO
- 고전역학
- write by GPT-4
- flet
- chatGPT's answer
- write by chatGPT
- 파이썬
- GPT-4's answer
- GIT
- Java
- JVM
- 역학
- 자바
- 인프라
- 자바암호
- 뉴턴역학
- python
- lombok
Archives
- Today
- Total
Akashic Records
Spring All Dependency Injection Types 본문
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
- Constructed object is immutable and returned to the client in a fully initialized state.
- An issue with a growing amount of dependencies is immediately visible with this approach. More dependencies bigger the constructor.
- Can be combined with setter injection or field injection, constructor parameters indicate required dependencies, others — optional.
Disadvantages
- No possibility to change object’s dependencies later — inflexibility.
- 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
- Flexibility in dependency resolution or object reconfiguration, it can be done anytime. Plus, this freedom solves the circular dependency issue of constructor injection.
Disadvantages
- Null checks are required, because dependencies may not be set at the moment.
- Potentially more error-prone and less secure than constructor injection due to the possibility of overriding dependencies.
Field injection — nobody likes it?
Advantages
- Easy to use, no constructors or setters required
- Can be easily combined with the constructor and/or setter approach
Disadvantages
- 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.
- A number of dependencies can reach dozens until you notice that something went wrong in your design.
- No immutability — the same as for setter injection.
Conclusion
- Constructor injection — good, reliable and immutable, inject via one of the constructors. Possible to configure in: XML, XML+Annotations, Java, Java + Annotations.
- Setter injection — more flexible, mutable objects, injection via setters. Possible to configure in: XML, XML+Annotations, Java, Java + Annotations.
- 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