9. ์ ํ๋ฆฌ์ผ์ด์ ์กฐ๋ฆฝํ๊ธฐ
1. ์กฐ๋ฆฝ๊น์ง ์ ๊ฒฝ ์จ์ผ ํ๋ ์ด์
- ์ฝ๋์ ์์กด์ฑ์ ํญ์ ์์ชฝ์ผ๋ก,
- ๋๋ฉ์ธ ์ฝ๋ ๋ฐฉํฅ์ผ๋ก ํฅํด์ผ ์์ ํจ.
- ์ค์ ์ปดํฌ๋ํธ (configuration component)๊ฐ ํ์
- ์ํคํ ์ฒ์ ์ค๋ฆฝ์
- ์ธ์คํด์ค ์์ฑ์ ์ํด ๋ชจ๋ ํด๋์ค์ ์์กด์ฑ์ ๊ฐ์ง

์ค์ ์ปดํฌ๋ํธ์ ์ญํ
- ์น ์ด๋ํฐ ์ธ์คํด์ค ์์ฑ
- HTTP ์์ฒญ์ด ์ค์ ๋ก ์น ์ด๋ํฐ๋ก ์ ๋ฌ๋๋๋ก ๋ณด์ฅ
- ์ ์ค์ผ์ด์ค ์ธ์คํด์ค ์์ฑ
- ์น ์ด๋ํฐ์ ์ ์ค์ผ์ด์ค ์ธ์คํด์ค ์ ๊ณต
- ์์์ฑ ์ด๋ํฐ ์ธ์คํด์ค ์์ฑ
- ์ ์ค์ผ์ด์ค์ ์์์ฑ ์ด๋ํฐ ์ธ์คํด์ค ์ ๊ณต
- ์์์ฑ ์ด๋ํฐ๊ฐ ์ค์ ๋ก ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ๊ทผํ ์ ์๋๋ก ๋ณด์ฅ
์ด๋ฌํ ์์ ์ ๋จ์ผ ์ฑ ์ ์์น์ ์๋ฐํ๋๊ฒ ๋ง๋ค.
์ ํ๋ฆฌ์ผ์ด์ ์ ๋ชจ๋ ๋ถํ์ ์๊ณ ์๊ธฐ ๋๋ฌธ.
2. ํ๋ฒํ ์ฝ๋๋ก ์กฐ๋ฆฝํ๊ธฐ
package com.book.cleanarchitecture.buckpal;
import com.book.cleanarchitecture.buckpal.account.adapter.in.web.SendMoneyController;
import com.book.cleanarchitecture.buckpal.account.adapter.out.persistence.AccountPersistenceAdapter;
import com.book.cleanarchitecture.buckpal.account.application.port.in.SendMoneyUseCase;
import com.book.cleanarchitecture.buckpal.account.application.service.SendMoneyService;
public class Application {
public static void main(String[] args) {
AccountRepository accountRepository = new AccountRepository();
ActivityRepository activityRepository = new ActivityRepository();
AccountPersistenceAdapter accountPersistenceAdapter = new AccountPersistenceAdapter(accountRepository, activityRepository);
SendMoneyUseCase sendMoneyUseCase = new SendMoneyService(
accountPersistenceAdapter,
accountPersistenceAdapter
);
SendMoneyController sendMoneyController = new SendMoneyController(sendMoneyUseCase);
startProcessingWebRequests(sendMoneyController);
}
}
- ์ค์ ์ปดํฌ๋ํธ์ ๊ฐ๋ตํ ์์
- main ๋ฉ์๋ ์์์ ๋ชจ๋ ํด๋์ค์ ์ธ์คํด์ค๋ฅผ ์์ฑ ํ ์ฐ๊ฒฐํ ๊ฒ์ ๋ณผ ์ ์๋ค.
- ์ ํ๋ฆฌ์ผ์ด์ ์ด ์์ ํด์ง ๊ฒฝ์ฐ ๋์ผํ ์ฝ๋๋ฅผ ๋์ ์์ด ์์ฑํด์ผ ํจ.
- ์ด๋ฌํ ๊ตฌ์ฑ์ ๋ชจ๋ ํด๋์ค public ์ด์ด์ผ ์ธ์คํด์ค ์์ฑ์ด ๊ฐ๋ฅํจ. => ์ต์
- package-private (default) ์ ๊ทผ ์ ํ์๋ฅผ ์ ์งํ๋ฉฐ ์๋ฐ์ ์์กด์ฑ ์ฃผ์ ํ๋ ์์ํฌ์ธ ์คํ๋ง์ ์ฌ์ฉํ ์๋ ์๋ค.
3. ์คํ๋ง์ ํด๋์คํจ์ค ์ค์บ๋์ผ๋ก ์กฐ๋ฆฝํ๊ธฐ
- ์คํ๋ง ํ๋ ์์ํฌ๋ฅผ ์ด์ฉํด ์กฐ๋ฆฝํ ์ ํ๋ฆฌ์ผ์ด์
๊ฒฐ๊ณผ๋ฌผ์ ์ ํ๋ฆฌ์ผ์ด์
์ปจํ
์คํธ๋ผ๊ณ ๋ถ๋ฆ.
- ๊ตฌ์ฑ ๊ฐ์ฒด๋ bean.
- ์คํ๋ง์ ํด๋์คํจ์ค ์ค์บ๋์ผ๋ก ํด๋์คํจ์ค์์ ์ ๊ทผ ๊ฐ๋ฅํ ๋ชจ๋ ํด๋์ค๋ฅผ ํ์ธํด์
@Component
์ ๋ํ ์ด์ ์ด ๋ถ์ ํด๋์ค๋ฅผ ์ฐพ๋๋ค.
์คํ๋ง ์ธ์ ์ฉ ์ปค์คํ ์ ๋ ธํ ์ด์
package com.book.cleanarchitecture.buckpal.shared;
import org.springframework.core.annotation.AliasFor;
import org.springframework.stereotype.Component;
import java.lang.annotation.*;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface UseCase {
@AliasFor(annotation = Component.class)
String value() default "";
}
- ์คํ๋ง์ด ์ธ์ํ ์ ์๋ ์ปค์คํ
์ ๋
ธํ
์ด์
๋ ์์ฑ ๊ฐ๋ฅํ๋ค.
๋จ์
- ์คํ๋ง ํ๋ ์์ํฌ์ ๋งค์ฐ ์์กด์ ์.
- ํ๋ง๋ฒ์ด ์ผ์ด๋ ๊ฐ๋ฅ์ฑ.
- ๋น์ฅ Bean์ผ๋ก ๋ฑ๋กํ์ง ์๊ธฐ๋ฅผ ๋ฐ๋ผ๋ ํด๋์ค๋ฅผ ๋ง์ง ๋ชปํจ.
- ์ ์์ ์ผ๋ก ์ ํ๋ฆฌ์ผ์ด์ ์ปจํ ์คํธ๋ฅผ ์กฐ์ํ์ฌ ์ถ์ ํ๊ธฐ ์ด๋ ค์ด ์๋ฌ๋ฅผ ์ผ์ผํฌ ์ฐ๋ ค๊ฐ ์๋ค.
4. ์คํ๋ง์ ์๋ฐ ์ปจํผ๊ทธ๋ก ์กฐ๋ฆฝํ๊ธฐ
@Configuration
์ ํตํด ์ค์บ๋์ ๊ฐ์ง๊ณ ์ฐพ์์ผ ํ๋ ์ค์ ํด๋์ค์์ ํ์.- ๋ชจ๋ Bean์ ์ฐพ์์ค๋๊ฑด ์๋๊ธฐ ๋๋ฌธ์, ๋ง๋ฒ์ด ์ผ์ด๋ ์ผ์ ์ ๋ค.
- ์ค์ ํด๋์ค์ ๊ฐ์ ํจํค์ง์ ๋ฃ์ด๋์ง ์๋ ๊ฒฝ์ฐ์ public์ผ๋ก ํด์ผ ํ๋ค. -> ๋จ์
- ํจํค์ง๋ฅผ ๋ชจ๋ ๊ฒฝ๊ณ๋ก ์ฌ์ฉํ๊ณ ๊ฐ ํจํค์ง ์์ ์ ์ฉ ํด๋์ค๋ฅผ ๋ง๋ค ์ ์์ง๋ง, ํ์ ํจํค์ง๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
5. ์ ์ง๋ณด์๊ฐ ๊ฐ๋ฅํ ์ํํธ์จ์ด๋ฅผ ๋ง๋๋ ๋ฐ ์ด๋ป๊ฒ ๋์์ด ๋๋์ง?
- ์คํ๋ง ํ๋ ์์ํฌ๋ฅผ ์ด์ฉํ์ฌ ํธ๋ฆฌํ๊ฒ ์ ํ๋ฆฌ์ผ์ด์ ์กฐ๋ฆฝ ๊ฐ๋ฅ.
- ์ฝ๋์ ๊ท๋ชจ๊ฐ ์ปค์ง ๊ฒฝ์ฐ ํฌ๋ช
์ฑ์ด ๋ฎ์์ง.
- ์ด๋ค Bean์ด ์ ํ๋ฆฌ์ผ์ด์ ์ปจํ ์คํธ์ ์ฌ๋ผ์ค๋์ง ์ ํํ ํ์ ๋ถ๊ฐ.
- ๋ ๋ฆฝ์ ์ธ ํ ์คํธ๊ฐ ๋ถ๊ฐ.
๋ฐ์ํ