๐[Spring] Spring์์ ๋น(Bean)์ ์ฃผ์ ๋ฐ๋ ๋ฐฉ๋ฒ๋ค.
- Spring์์ ๋น(Bean) ์ ์ฃผ์ ๋ฐ๋ ๋ฐฉ๋ฒ์๋ ์ฌ๋ฌ ๊ฐ์ง๊ฐ ์์ผ๋ฉฐ, ์ฃผ๋ก ์์กด์ฑ ์ฃผ์ (Dependency Injection, DI) ์ด๋ผ๋ ๊ฐ๋ ์ ํตํด ์ด๋ฃจ์ด์ง๋๋ค.
- Spring IoC ์ปจํ ์ด๋๋ ๊ฐ์ฒด ๊ฐ์ ์์กด์ฑ์ ๊ด๋ฆฌํ๊ณ , ํ์ํ ๊ณณ์ ์๋์ผ๋ก ๋น์ ์ฃผ์ ํฉ๋๋ค.
- ๋น์ ์ฃผ์ ๋ฐ๋ ๋ฐฉ๋ฒ์๋ ์์ฑ์ ์ฃผ์ , ์ธํฐ ์ฃผ์ , ํ๋ ์ฃผ์ ์ด ์์ต๋๋ค.
๐โโ๏ธ ์์กด์ฑ(Dependency)
๐โโ๏ธ Spring ์ปจํ ์ด๋๋ฅผ ์ฌ์ฉํ๋ ์ด์
๐โโ๏ธ Spring ์ปจํ ์ด๋
๐โโ๏ธ Spring ๋น(Bean)
1๏ธโฃ ์์ฑ์ ์ฃผ์ (Constructor Injection)
- ์์ฑ์ ์ฃผ์ ์ ์์กด์ฑ์ ์ฃผ์ ํ ๋ ์์ฑ์๋ฅผ ํตํด ๋น์ ์ฃผ์ ํ๋ ๋ฐฉ์์ ๋๋ค.
- ๊ฐ์ฅ ๊ถ์ฅ๋๋ ๋ฐฉ์ ์ค ํ๋๋ก, ์์กด์ฑ์ ๊ฐ์ ํ๊ณ ๋ถ๋ณ์ฑ์ ๋ณด์ฅํ ์ ์์ต๋๋ค.
- ๋ํ, ํ ์คํธํ๊ธฐ ์ฉ์ดํ ๋ฐฉ์์ ๋๋ค.
์์
@Service
public class UserService {
private final UserRepository userRepository;
@Autowired // Spring 4.3+ ์์๋ ์๋ต ๊ฐ๋ฅ
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public void process() {
userRepository.save();
}
}
์ค๋ช .
-
UserService
๋UserRepository
๋น์ ์์ฑ์๋ฅผ ํตํด ์ฃผ์ ๋ฐ์ต๋๋ค. -
@Autowired
๋ฅผ ํตํด ์คํ๋ง์ดUserRepository
๋น์ ์๋์ผ๋ก ์ฃผ์ ํ๊ฒ ๋ฉ๋๋ค. -
@Autowired
๋ Spring 4.3 ์ดํ ์์ฑ์ ์ฃผ์ ์์๋ ์๋ต ๊ฐ๋ฅํ์ง๋ง, ๋ช ์์ ์ผ๋ก ์ ๋ ๊ฒฝ์ฐ๋ ์์ต๋๋ค.
2๏ธโฃ ์ธํฐ ์ฃผ์ (Setter Injection)
- ์ธํฐ ์ฃผ์ ์ ์ธํฐ ๋ฉ์๋๋ฅผ ํตํด ๋น์ ์ฃผ์ ํ๋ ๋ฐฉ์์ ๋๋ค.
- ์ ํ์ ์ธ ์์กด์ฑ์ ์ฃผ์ ํ ๋ ์ ์ฉํ๋ฉฐ, ์ฃผ์ ๋ฐ์ ๋น์ ๋ณ๊ฒฝํ ์ ์๋ ์ ์ฐ์ฑ์ ์ ๊ณตํฉ๋๋ค.
์์
@Service
public class UserService {
private UserRepository userRepository;
@Autowired
public void setUserRepository(UserRepository userRepository) {
this.userRespository = userRepository;
}
public void process() {
userRepository.sava();
}
}
์ค๋ช .
-
UserSevice
๋setUserRepository
๋ผ๋ ์ธํฐ ๋ฉ์๋๋ฅผ ํตํดUserRepository
๋น์ ์ฃผ์ ๋ฐ์ต๋๋ค.-
@Autowired
์ด๋ ธํ ์ด์ ์ ํตํด ์คํ๋ง์ด ์ ์ ํ ๋น์ ์ฃผ์ ํ๊ฒ ๋ฉ๋๋ค.
-
3๏ธโฃ ํ๋ ์ฃผ์ (Field Injection)
- ํ๋ ์ฃผ์
์ ์ง์ ํ๋์
@Autowired
์ด๋ ธํ ์ด์ ์ ๋ถ์ฌ์ ๋น์ ์ฃผ์ ํ๋ ๋ฐฉ์์ ๋๋ค. - ๊ฐ์ฅ ๊ฐ๋จํ ๋ฐฉ์์ด์ง๋ง, ํ ์คํธํ๊ธฐ ์ด๋ ค์ด ๊ตฌ์กฐ๋ฅผ ๋ง๋ค ์ ์๊ณ , ์ฃผ์ ๋ ํ๋๊ฐ โfinalโ๋ก ์ค์ ๋์ง ์๊ธฐ ๋๋ฌธ์ โ๋ถ๋ณ์ฑโ์ด ๋ณด์ฅ๋์ง ์์ต๋๋ค.
- ์ผ๋ฐ์ ์ผ๋ก๋ ์ง์ํ๋ ๋ฐฉ์์ ๋๋ค.
์์
@Servicee
public class UserService {
@Autowired
private UserRepository userRepository;
public void process() {
userRepository.save();
}
}
์ค๋ช .
-
UserService
๋UserRepository
๋น์ ํ๋์ ์ง์ ์ฃผ์ ๋ฐ์ต๋๋ค. - ํ๋ ์ฃผ์ ๋ฐฉ์์ ์ฝ๋๊ฐ ๊ฐ๊ฒฐํ์ง๋ง, ํ ์คํธ๋ ์ ์ง๋ณด์ ์ธก๋ฉด์์ ๋ถ๋ฆฌํ ์ ์์ต๋๋ค.
4๏ธโฃ ๊ฐ ์ฃผ์ ๋ฐฉ์์ ๋น๊ต.
1. ์์ฑ์ ์ฃผ์ (Constructor Injection)
-
์ฅ์
- ์์กด์ฑ์ด ํ์์ ์์ ๊ฐ์ ํ ์ ์๊ณ , ๋ถ๋ณ์ฑ์ ๋ณด์ฅํ๋ฉฐ, ํ ์คํธํ๊ธฐ ์ฉ์ดํฉ๋๋ค.
- ์์กด์ฑ์ด ์ฃผ์ ๋์ง ์์ผ๋ฉด ์ปดํ์ผ ํ์์ ์ค๋ฅ๋ฅผ ๋ฐ๊ฒฌํ ์ ์์ต๋๋ค.
-
๋จ์
- ํด๋์ค๊ฐ ๋ง์ ์์กด์ฑ์ ๊ฐ์ง ๊ฒฝ์ฐ, ์์ฑ ์ธ์๊ฐ ๋ง์์ง ์ ์์ต๋๋ค.
2. ์ธํฐ ์ฃผ์ (Setter Injection)
-
์ฅ์
- ์ ํ์ ์ธ ์์กด์ฑ ์ฃผ์ ์ด ๊ฐ๋ฅํ๋ฉฐ, ๊ฐ์ฒด ์์ฑ ํ์ ์ฃผ์ ํ ์ ์์ด ์ ์ฐ์ฑ์ ์ ๊ณตํฉ๋๋ค.
-
๋จ์
- ์์กด์ฑ์ด ์ฃผ์ ๋์ง ์์ ์ํ๋ก ์ฌ์ฉ๋ ์ํ์ด ์กด์ฌํ๋ฉฐ, ๊ฐ์ฒด์ ์ํ๊ฐ ๋ณ๊ฒฝ๋ ์ ์์ต๋๋ค.
3. ํ๋ ์ฃผ์ (Field Injection)
-
์ฅ์
- ์ฝ๋๊ฐ ๊ฐ๊ฒฐํ๊ณ ๊ฐ์ฅ ์ฝ์ต๋๋ค.
-
๋จ์
- ํ ์คํธํ๊ธฐ ์ด๋ ต๊ณ , ์์กด์ฑ์ ๊ฐ์ ํ์ง ์์ผ๋ฉฐ, ๋ฆฌํ๋ ์ ์ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ๋ถ๋ณ์ฑ์ด ๋ณด์ฅ๋์ง ์์ต๋๋ค.
- ๋ํ, ํ๋์ ์ ๊ทผํ๋ ๋ฐฉ์์ด๊ธฐ ๋๋ฌธ์ SRP(Single Responsibility Principle)๋ฅผ ์๋ฐํ ๊ฐ๋ฅ์ฑ์ด ๋์ต๋๋ค.
๐โโ๏ธ SOLID ์์น
5๏ธโฃ ๊ฒฐ๋ก
- ์์ฑ์ ์ฃผ์ (Constructor Injection) : ์์ฑ์ ์ฃผ์ (Constructor Injection) ์ ์์กด์ฑ ๊ฐ์ , ๋ถ๋ณ์ฑ ๋ณด์ฅ, ํ ์คํธ ์ฉ์ด์ฑ ์ธก๋ฉด์์ ๊ฐ์ฅ ๊ถ์ฅ๋๋ ๋ฐฉ์์ ๋๋ค.
- ์ธํฐ ์ฃผ์ (Setter Injection) : ์ ํ์ ์์กด์ฑ์ ์ฃผ์ ํ ๋ ์ ์ฉํ์ง๋ง, ์ธํฐ ๋ฉ์๋๊ฐ ๊ณต์ฉ์ผ๋ก ๋ ธ์ถ๋๋ค๋ ๋จ์ ์ด ์์ต๋๋ค.
- ํ๋ ์ฃผ์ (Field Injection) : ๊ฐ์ฅ ๊ฐ๋จํ ๋ฐฉ์์ด์ง๋ง, ํ ์คํธ๊ฐ ์ด๋ ต๊ณ ๋ถ๋ณ์ฑ์ ๋ณด์ฅํ์ง ์๊ธฐ ๋๋ฌธ์ ์ง์ํ๋ ๋ฐฉ์์ ๋๋ค.