1. ์์กด์ฑ ์ฃผ์ (Dependency Injection) ์ข ๋ฅ
์์กด์ฑ ์ฃผ์ ์ ์ข ๋ฅ๋ก๋ Field, Setter, Constructor(์์ฑ์) ๋ฐฉ์์ด ์๋ค.
1) Field Injection
public class AccountController{
@Autowired
AccountService accountService;
@Autowired
AccountRepository accountRepository;
}
2) Setter Injection
public class AccountConroller{
private AccountService accountService;
private AccountRepository accountRepository;
@Autowired
pubic void setAccountService(AccountService accountService){
this.accountService = accountService;
}
@Autowired
pubic void setAccountRepository(AccountRepository accountRepository){
this.accountRepository = accountRepository;
}
}
3) Constructor Injection
public class AccountConroller{
private AccountService accountService;
private AccountRepository accountRepository;
@Autowired
pubic void setAccountService(AccountService accountService, AccountRepository accountRepository){
this.accountService = accountService;
this.accountRepository = accountRepository;
}
}
2. Constructor Injection ์ ๊ถ์ฅํ๋ ์ด์
Spring ์์๋ Constructor Injection์ ๊ถ์ฅํ๋ค.
The Spring team generally advocates constructor injection as it enables one to implement application components as immutable objects and to ensure that required dependencies are not null . Furthermore constructor-injected components are always returned to client (calling) code in a fully initialized state. As a side note, a large number of constructor arguments is a bad code smell , implying that the class likely has too many responsibilities and should be refactored to better address proper separation of concerns.
- ๋ถ๋ณ์ฑ(immutable)
Constructor Injection๋ง ์ ์ผํ๊ฒ final๋ก ์ ์ธ์ด ๊ฐ๋ฅํ๋ค. Field, Setter ๋ฐฉ์์ ๊ฒฝ์ฐ, finalํค์๋๋ฅผ ์ฌ์ฉํ๋ฉด ์ปดํ์ผ ์๋ฌ๊ฐ ๋๋ค. final๋ก ์ ์ธ๋ ๊ฐ์ฒด๋ ์ฐธ์กฐ์ ์ํ ๋ณ๊ฒฝ์ด ์ด๋ฃจ์ด ์ง ์ ์๊ธฐ ๋๋ฌธ์ ๊ทธ๋ก ์ธํด ๋ฐ์๋ ์ ์๋ ์๋ฌ๋ฅผ ๋ฐฉ์งํ ์ ์๋ค. - NULL ๋ฐฉ์ง
์์ ๊ฐ์ ์ด์ ๋ก final๋ก ์ ์ธ๋ ๋ณ์๋ ์ค๊ฐ์ ๋ค๋ฅธ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ ์ ์์ผ๋ฏ๋ก ์ฃผ์ ๋ฐ๋ ๊ฐ์ฒด๊ฐ null์ด ์๋๋ผ๋ฉด, Null์ด ์๋์ ๋ณด์ฅํ ์ ์๋ค. - ์ํ ์ฐธ์กฐ ๋ฐฉ์ง
์ํ ์ฐธ์กฐ๋ Aํด๋์ค๊ฐ Bํด๋์ค๋ฅผ ์ฐธ์กฐํ๊ณ ๋ค์ Bํด๋์ค๊ฐ Aํด๋์ค๋ฅผ ์ฐธ์กฐํ๋ ๊ฒฝ์ฐ๋ฅผ ๋งํ๋ค. ์ํ ์ฐธ์กฐ ๋ฌธ์ ๋ ์์ฑ์ ์ฃผ์ ์ ์ด์ฉํ ๋ ๊ฐ์งํ ์ ์๋ค. ์๋ฒ ์์ฒด๊ฐ ๊ตฌ๋๋์ง ์๋๋ค.
์ด๋ฐ ์ฐจ์ด๊ฐ ์๋ ์ด์ ๋ ์์ฑ์ ์ฃผ์ ๋ฐฉ๋ฒ์ ํ๋ ์ฃผ์ ์ด๋ ์์ ์ ์ฃผ์ ๊ณผ๋ ๋น(bean)์ ์ฃผ์ ํ๋ ์์๊ฐ ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ด๋ค.
3. bean ์ฃผ์ ์์
- Setter, Field Injection ๋ฐฉ์
1) ๋ชจ๋ ๋น(bean)์ ์ฐ์ ๋ง๋ค๊ณ BeanFactory์ ๋ฑ๋กํ๋ค.
2) ๋น(bean)์ ์ฃผ์ ๋ฐ์์ผ ํ๋ A ๊ฐ์ฒด์ ํ์ํ ๋น(bean)๋ค์ ์ฃผ์ ํ๋ค. - ์์ฑ์ ์ฃผ์
(Constructor Injection) ๋ฐฉ์
1) ํด๋์ค A ๋น(bean) ์์ฑ์ ์์ฑ์๋ฅผ ์ด์ฉํ๋ฉฐ ์ด๋ ํ์ํ ๋น(bean)์ธ B ๊ฐ์ฒด๋ฅผ ์ฃผ์ ํ๋ ค๊ณ ์๋ํ๋ค.
2) ํ์ํ B ๊ฐ์ฒด๋ ์กด์ฌํ์ง ์์ผ๋ฏ๋ก ์ด๋ฅผ ์์ฑ์๋ก ๋ง๋ค๋ ค๊ณ ์๋ํ๋ค.
3) B ๊ฐ์ฒด๋ ์์ฑํ๋ ์์ ์ A ๊ฐ์ฒด๊ฐ ํ์ํ๋ค.
4) ์ํ ์ฐธ์กฐ๊ฐ ๊ฐ์ง๋ฉ๋๋ค.
4. @RequiredArgsConstructor
์ด ์ด๋ ธํ ์ด์ ์ ์ด๊ธฐํ ๋์ง์์ final ํ๋๋, @NonNull ์ด ๋ถ์ ํ๋์ ๋ํด ์์ฑ์๋ฅผ ์์ฑํด ์ค๋ค.
์์ฑ์๊ฐ ํ๋๋ง ์๊ณ , ๊ทธ ์์ฑ์๊ฐ ๋ฐ๋ param์ด bean์ผ๋ก ๋ฑ๋ก๋์ด ์๋ค๋ฉด ์๋์ผ๋ก bean์ ์ฃผ์ ํด์ฃผ๊ธฐ ๋๋ฌธ์ @autowired ๊ฐ์ ์ด๋ ธํ ์ด์ ์์ด๋ bean์ผ๋ก ์์กด์ฑ ์ฃผ์ ์ด ๋๋ค.
[AS-IS]
@Component
public class AccountConroller{
private AccountService accountService;
private AccountRepository accountRepository;
@Autowired
pubic void setAccountService(AccountService accountService, AccountRepository accountRepository){
this.accountService = accountService;
this.accountRepository = accountRepository;
}
}
[TO-BE]
@Component
@RequiredArgsConstructor
public class SignUpFormValidator implements Validator {
private AccountService accountService;
private AccountRepository accountRepository;
}
์ฐธ๊ณ ์๋ฃ
https://velog.io/@sloools/Spring-์์กด์ฑ-์ฃผ์ -์ข ๋ฅ-๋ฐRequiredArgsConstructor-์ด๋ ธํ ์ด์
https://junhyunny.github.io/spring-boot/junit/reson-of-recommendation-to-use-constructor-injection/
'SRPING' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JPA ๊ฐ์ฒด์ ์ํ / @Transactional (0) | 2023.01.30 |
---|---|
Model Binding : WebDataBinder, @InitBinder (0) | 2023.01.18 |
Thymeleaf (0) | 2023.01.15 |