๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿ’Š Java & Kotlin & Spring/- spring framework +

[Spring Framework] Spring-Core(BeanFactory ์ธํ„ฐํŽ˜์ด์Šค) ๋ถ„์„ ๋ฐ ๊ตฌํ˜„

by Wonit 2020. 9. 7.
์ด ๊ธ€์€ Spring์˜ ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ํ•ด์„ํ•˜๊ณ  ์‰ฝ๊ฒŒ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•œ ๊ธ€์ด๋ฏ€๋กœ ๊ณต์‹ ๋ฌธ์„œ์— ๊ฐœ์ธ์˜ ์ƒ๊ฐ๊ณผ ์ถ”์ธก์ด ํฌํ•จ๋œ ๊ธ€์ด๋‹ˆ ์ด ์  ์ฐธ๊ณ ํ•˜๊ณ  ์ฝ์–ด์ฃผ์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.

์˜ค๋Š˜ ์•Œ์•„๋ณผ ๊ฒƒ์€ Spring-Framework-Core์˜ BeanFactory์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณผ ๊ฒƒ์ด๋‹ค.

Interface BeanFactory

Spring ๊ณต์‹ ๋ฌธ์„œ์—์„œ๋Š” BeanFactory์ธํ„ฐํŽ˜์ด์Šค๋ฅผ Spring ์ปจํ…Œ์ด๋„ˆ์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•œ Root ์ธํ„ฐํŽ˜์ด์Šค๋ผ๊ณ  ์ •์˜ํ•œ๋‹ค.

์ด ์ธํ„ฐํŽ˜์ด์Šค๋Š” ๊ฐ๊ฐ String ์ด๋ฆ„์œผ๋กœ ๊ณ ์œ ํ•˜๊ฒŒ ์‹๋ณ„๋˜๋Š” ์—ฌ๋Ÿฌ Bean ์ •์˜๋ฅผ ๋ณด์œ ํ•˜๋Š” ์˜ค๋ธŒ์ ํŠธ์— ์˜ํ•ด ๊ตฌํ˜„๋ฉ๋‹ˆ๋‹ค. ๋นˆ ์ •์˜์— ๋”ฐ๋ผ ํŒฉํ† ๋ฆฌ๋Š” ํฌํ•จ ๋œ ๊ฐ์ฒด์˜ ๋…๋ฆฝ ์ธ์Šคํ„ด์Šค (Prototype ๋””์ž์ธ ํŒจํ„ด) ๋˜๋Š” ๋‹จ์ผ ๊ณต์œ  ์ธ์Šคํ„ด์Šค (์ธ์Šคํ„ด์Šค๊ฐ€ ๋ฒ”์œ„์˜ ์‹ฑ๊ธ€ ํ†ค ์ธ Singleton ๋””์ž์ธ ํŒจํ„ด์˜ ์šฐ์ˆ˜ํ•œ ๋Œ€์•ˆ)๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ณต์žฅ). ๋ฆฌํ„ด๋˜๋Š” ์ธ์Šคํ„ด์Šค ์œ ํ˜•์€ Bean ํŒฉํ† ๋ฆฌ ๊ตฌ์„ฑ์— ๋”ฐ๋ผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. API๋Š” ๋™์ผํ•ฉ๋‹ˆ๋‹ค. Spring 2.0๋ถ€ํ„ฐ๋Š” ๊ตฌ์ฒด์ ์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ (์˜ˆ : ์›น ํ™˜๊ฒฝ์˜ "request"๋ฐ "session"๋ฒ”์œ„)์— ๋”ฐ๋ผ ์ถ”๊ฐ€ ๋ฒ”์œ„๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ฒฐ๋ก ์€ BeanFactory๋Š” Singleton ํŒจํ„ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” Spring์˜ ์ปจํ…Œ์ด๋„ˆ ์ด๋‹ค.

์ด๋Ÿฐ BeanFactory๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ค‘์•™ ๋ ˆ์ง€์ŠคํŠธ๋ฆฌ ์—ญํ• ์„ ํ•œ๋‹ค.

๋˜ ์•„๋ž˜๋ฅผ ์ฝ์–ด๋ณด๋ฉด,
์ผ๋ฐ˜์ ์œผ๋กœ BeanFactory ์กฐํšŒ์™€ ๊ฐ™์€ "ํ’€ (pull)"๊ตฌ์„ฑ ํ˜•์‹์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค setter ๋˜๋Š” ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ ๊ฐœ์ฒด๋ฅผ ๊ตฌ์„ฑํ•˜๋ ค๋ฉด ์ข…์†์„ฑ ์ฃผ์ž… ( "ํ‘ธ์‹œ"๊ตฌ์„ฑ)์— ์˜์กดํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. Spring์˜ Dependency Injection ๊ธฐ๋Šฅ์€์ด BeanFactory ์ธํ„ฐํŽ˜์ด์Šค์™€ ํ•˜์œ„ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„๋ฉ๋‹ˆ๋‹ค

์ด๋Š” DL(Dependency Lookup)๋ณด๋‹ค๋Š” DI(Dependency Injection)์˜ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ๋”์šฑ ์ข‹๋‹ค๊ณ  ํ•œ๋‹ค.

Interface BeanFactory ์˜ Method

๋‹ค์–‘ํ•˜๊ฒŒ BeanFactory์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ์กด์žฌํ•˜๋Š”๋ฐ, ์šฐ๋ฆฌ๋Š” ๊ฐ„๋‹จํ•œ IOC ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒƒ๋“ค

๋Œ“๊ธ€