티스토리 뷰
There are two different way to inject spring beans using @Autowired and Constructor. When it comes to using a constructor to inject a bean, Lombok is a useful way to reduce some boilerplate code. However, if fields order within a method is changed, parameters order also would be changed, so that error would occur. You can use the constructor instead of @Autowired to prevent the error.
@Autowired를 사용하지 않아도 객체를 주입하는 방법인 Constructor(생성자)를 이용하는 방법이 있는데 Lombok annotation인 @AllArgsConstructor 를 이용해서 Constructor조차 생략할 수가 있다. 그러나 이 경우 안에 filed순서가 바뀌면 Parameter 의 순서까지 바뀌게 되어 program에서 error가 발생할 가능성이 있다.
그러니 @Autowired대신 Constructor를 사용하되 Lombok으로 대신하지말고 그대로 생성자를 적어주자.
@RestController
//@Autowired 대신 객체를 주입하는 방법인 생성자따로 적을 필요없는 Lombok annotation 그러나 인자의 순서가 바뀌어 error를 발생시킬 수 있음.
//@AllArgsConstructor
@RequestMapping("/travels")
public class TravelController {
private final TravelRepository travelRepository;
public TravelController(TravelRepository travelRepository){
this.travelRepository = travelRepository;
}
'Spring Boot' 카테고리의 다른 글
@Builder annotation (0) | 2019.03.22 |
---|---|
DTO 의 @Data와 @Setter (0) | 2019.03.18 |
Id type은 왜 Long 일까 (0) | 2019.03.10 |
DTO(Data Transfer Object) 와 ModelMapper (0) | 2019.03.10 |
Constructor Injection (0) | 2019.03.08 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- algorithm
- 알고리즘
- ... in Javascript
- 프로그래머스
- Javascript Algorithm
- string class in java
- 프로그래머스 알고리즘
- hackerrank javascript solution
- math.abs
- rest parameter
- code refactoring
- java
- easy algorithm
- spring boot application
- easy javascript algorithm
- math.max
- hackerrank solution
- equals()
- C++
- hackerrank javascript
- javascript
- compareTo()
- HackerRank Algorithm
- 프로그래머스 알고리즘문제
- HashMap
- substring()
- Object type casting
- repeat()
- hackerrank
- Collection Framework
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함