Search
Duplicate
โ›ณ

DI

stage 0

/** * UserServiceํด๋ž˜์Šค์˜join๋ฉ”์„œ๋“œ๋Š” ์ •์  ๋ฉ”์„œ๋“œ๋‹ค. *๊ฐ์ฒด์ง€ํ–ฅ์ ์ด์ง€ ์•Š์€ ์ฝ”๋“œ๋Š” ๋ณ€๊ฒฝ์— ์ทจ์•ฝํ•˜๋‹ค. * UserService๋Š”UserDao์™€ ๋ฐ€์ ‘ํ•˜๊ฒŒ ๊ฒฐํ•ฉ๋˜์–ด ์žˆ๋Š”๋ฐ ์ด๋Š” ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์–ด๋ ต๊ฒŒ ๋งŒ๋“ ๋‹ค. *ํ…Œ์ŠคํŠธDB๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์ฝ”๋“œ์˜ ๋งŽ์€ ๋ถ€๋ถ„์ด ์ˆ˜์ •๋˜์–ด์•ผ ํ•œ๋‹ค. */
Plain Text
๋ณต์‚ฌ
final var user = new User(1L, "gugu"); final var actual = UserService.join(user); assertThat(actual.getAccount()).isEqualTo("gugu");
Java
๋ณต์‚ฌ

stage 1

/** * 0๋‹จ๊ณ„์˜ ์ฃผ์š” ๋ฌธ์ œ๋Š” ์ •์  ๋ฉ”์„œ๋“œ๋งŒ ์žˆ์–ด์„œ ํด๋ž˜์Šค ๊ฐ„์— ๋งค์šฐ ๊ธด๋ฐ€ํ•œ ๊ฒฐํ•ฉ์ด ๋ฐœ์ƒํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. *์šฐ๋ฆฌ๋Š” ์˜์กดํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ํ•„์š”์— ๋”ฐ๋ผ ๊ต์ฒดํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค. *๋‹ค๋ฅธ ๊ตฌํ˜„์ฒด๋กœ ๊ต์ฒดํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“ค์–ด๋ณด์ž. */
Plain Text
๋ณต์‚ฌ
/** *ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ ์ง์ ‘ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์ง€ ๋ง๊ณ  ์™ธ๋ถ€์—์„œ ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌ ๋ฐ›๋„๋ก ์ˆ˜์ •ํ–ˆ๋‹ค. *์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด์„œ ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌ ๋ฐ›๋„๋ก ์ˆ˜์ •ํ–ˆ๋‹ค. *ํ•˜์ง€๋งŒ ์•„์ง๋„ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์–ด๋ ค์šด ์ฝ”๋“œ๋‹ค. * UserDao๋ผ๋Š” ๊ตฌํ˜„ ํด๋ž˜์Šค์— ์˜์กดํ•˜๊ณ  ์žˆ๋‹ค. *์œ ์—ฐํ•œ ๋ณ€๊ฒฝ์ด ํ•„์š”ํ•œ ๋ถ€๋ถ„์€ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฒฐํ•ฉ์„ ๊ฐ์†Œ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. */
Plain Text
๋ณต์‚ฌ
final var user = new User(1L, "gugu"); final var userDao = new UserDao(); final var userService = new UserService(userDao); final var actual = userService.join(user); assertThat(actual.getAccount()).isEqualTo("gugu");
Java
๋ณต์‚ฌ

stage 2

/** *์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒฐํ•ฉ๋„๋ฅผ ๋‚ฎ์ท„๋‹ค. *์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉดtestAnonymousClassํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์ฒ˜๋Ÿผ ๊ฐœ๋ฐœ์ž๊ฐ€ ๊ตฌํ˜„ ๊ฐ์ฒด๋ฅผ ๋ฐ”๊ฟ”์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. *ํ•˜์ง€๋งŒ ์•„์ง๋„ ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค. *์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„ ๊ฐ์ฒด๋ฅผ ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ๊ฒฐ์ •ํ•ด์ค˜์•ผ ํ•œ๋‹ค. *๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ์—ญํ• ์ด ํ•„์š”ํ•˜๋‹ค. */
Plain Text
๋ณต์‚ฌ
void stage2() { final var user = new User(1L, "gugu"); final UserDao userDao = new InMemoryUserDao(); final var userService = new UserService(userDao); final var actual = userService.join(user); assertThat(actual.getAccount()).isEqualTo("gugu"); }
Java
๋ณต์‚ฌ
void testAnonymousClass() { // given final var userDao = new UserDao() { private User user; @Override public void insert(User user) { this.user = user; } @Override public User findById(long id) { return user; } }; final var userService = new UserService(userDao); final var user = new User(1L, "gugu"); // when final var actual = userService.join(user); // then assertThat(actual.getAccount()).isEqualTo("gugu"); }
Java
๋ณต์‚ฌ

stage 3

DIContainer

โ€ข
์Šคํ”„๋ง์˜ BeanFactory, ApplicationContext์— ํ•ด๋‹น๋˜๋Š” ํด๋ž˜์Šค
class DIContainer { private final Set<Object> beans; public DIContainer(final Set<Class<?>> classes) { this.beans = Set.of(); } @SuppressWarnings("unchecked") public <T> T getBean(final Class<T> aClass) { return null; } }
Java
๋ณต์‚ฌ
@SuppressWarnings ์˜ ๊ธฐ๋Šฅ์€?
/** *๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ์—ญํ• ์„DIContainer๋ผ๋Š” ํด๋ž˜์Šค์—๊ฒŒ ๋งก๊ธฐ์ž. * DIContainer๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ตฌ์„ฑํ•˜๋Š” ๊ฐ์ฒด์˜ ๊ตฌ์กฐ์™€ ๊ด€๊ณ„๋ฅผ ์ •์˜ํ•œ ์„ค๊ณ„๋„ ์—ญํ• ์„ ํ•œ๋‹ค. *์–ด๋–ค ๊ฐ์ฒด๊ฐ€ ์–ด๋–ค ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋Š”์ง€ ์ •์˜ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. */
Plain Text
๋ณต์‚ฌ
/** *์ œ์–ด์˜ ์—ญ์ „(IoC) * stage2์—์„œ ๊ฐ์ฒด๋Š” ๋Šฅ๋™์ ์œผ๋กœ ์ž์‹ ์ด ์‚ฌ์šฉํ•  ํด๋ž˜์Šค๋ฅผ ๊ฒฐ์ •ํ•˜๊ณ ,์ง์ ‘ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ–ˆ๋‹ค. * ํ•˜์ง€๋งŒ ์ œ์–ด์˜ ์—ญ์ „์ด๋ผ๋Š” ๊ฐœ๋…์ด ์ ์šฉ๋˜๋ฉด ๊ฐ์ฒด๋Š” ์ž์‹ ์ด ์‚ฌ์šฉํ•  ๊ฐ์ฒด๋ฅผ ์„ ํƒํ•˜๊ณ  ์ƒ์„ฑํ•˜์ง€ ์•Š๋Š”๋‹ค. * ๋ชจ๋“  ์ œ์–ด ๊ถŒํ•œ์„ ์ž์‹ ์ด ์•„๋‹Œ ๋‹ค๋ฅธ ๋Œ€์ƒ์—๊ฒŒ ์œ„์ž„ํ•œ๋‹ค. * UserService๋Š”DIContainer์—๊ฒŒ ๋ชจ๋“  ์ œ์–ด ๊ถŒํ•œ์„ ์œ„์ž„ํ•œ ์ƒํƒœ๋‹ค. * DIContainer๊ฐ€ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•˜๋„๋ก ๊ตฌํ˜„ํ•ด๋ณด์ž. */ DIContainer๊ฐ€ ๊ด€๋ฆฌํ•˜๋Š” ๊ฐ์ฒด๋Š” ๋นˆ(bean) ๊ฐ์ฒด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.
Plain Text
๋ณต์‚ฌ
ApplicationContext ๊ตฌํ˜„์ฒด๋กœ ๋“ฑ๋ก๋˜๋Š” ํด๋ž˜์Šค๋Š” AnnotationConfigServletWebServerApplicationContext
BeanFactory ๊ตฌํ˜„์ฒด๋กœ ๋“ฑ๋ก๋˜๋Š” ํด๋ž˜์Šค๋Š”
DefaultListableBeanFactory