Skip to content

Latest commit

ย 

History

History
51 lines (40 loc) ยท 3.45 KB

README.md

File metadata and controls

51 lines (40 loc) ยท 3.45 KB

Bean

Bean์ด๋ž€ ์ปจํ…Œ์ด๋„ˆ ์•ˆ์— ๋“ค์–ด์žˆ๋Š” ์ž๋ฐ” ๊ฐ์ฒด๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
์ปจํ…Œ์ด๋„ˆ์— ๋‹ด๊ฒจ์žˆ์œผ๋ฉฐ, ํ•„์š”ํ•  ๋•Œ ์ปจํ…Œ์ด๋„ˆ์—์„œ ๊ฐ€์ ธ์™€์„œ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
@Bean์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ xml ์„ค์ •์„ ํ†ตํ•ด ์ผ๋ฐ˜ ๊ฐ์ฒด๋ฅผ Bean์œผ๋กœ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ๊ณ , Bean์œผ๋กœ ๋“ฑ๋ก๋œ ๊ฐ์ฒด๋Š” ์‰ฝ๊ฒŒ ์ฃผ์ž…ํ•˜์—ฌ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ’ก Bean ์ƒ๋ช… ์ฃผ๊ธฐ
์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ์˜ํ•ด ์ƒ๋ช…์ฃผ๊ธฐ๊ฐ€ ๊ด€๋ฆฌ๋˜๋ฉฐ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ˆœ์„œ๋กœ ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค.
1๏ธโƒฃ ๊ฐ์ฒด ์ƒ์„ฑ: ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ์ดˆ๊ธฐํ™” ์‹œ ๋นˆ ๊ฐ์ฒด ์ƒ์„ฑ
2๏ธโƒฃ ์˜์กด ์„ค์ •: ์˜์กด ๊ฐ์ฒด ์ฃผ์ž…
3๏ธโƒฃ ์ดˆ๊ธฐํ™”
4๏ธโƒฃ ์‚ฌ์šฉ
5๏ธโƒฃ ์†Œ๋ฉธ: ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ์ข…๋ฃŒ ์‹œ ๋นˆ ๊ฐ์ฒด ์†Œ๋ฉธ

์ดˆ๊ธฐํ™” ๋ฐ ์†Œ๋ฉธ ๋ฐฉ๋ฒ• 3๊ฐ€์ง€

1๏ธโƒฃ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์ด ๊ฐ„๊ฒฐํ•˜๋ฉฐ ์ฝ”๋“œ์—์„œ ์ดˆ๊ธฐํ™” ๋ฉ”์†Œ๋“œ๊ฐ€ ์กด์žฌํ•จ์„ ์‰ฝ๊ฒŒ ํŒŒ์•… ๊ฐ€๋Šฅํ•˜์—ฌ xml ์„ค์ • ๋ฐฉ๋ฒ•๋ณด๋‹ค ์ง๊ด€์ ์ž…๋‹ˆ๋‹ค. (๊ถŒ์žฅ)
์ดˆ๊ธฐํ™”: ๋นˆ ์ดˆ๊ธฐํ™” ๋ฉ”์†Œ๋“œ์— @PostConstruct ์‚ฌ์šฉํ•˜์—ฌ xml์— <context:annotation-config></context:annotation-config> ์ถ”๊ฐ€
์†Œ๋ฉธ: ๋นˆ ์†Œ๋ฉธ ๋ฉ”์†Œ๋“œ์— @PreDestroy ์‚ฌ์šฉํ•˜์—ฌ xml์— <context:annotation-config></context:annotation-config> ์ถ”๊ฐ€

2๏ธโƒฃ ๊ฐ„๊ฒฐํ•˜์ง€ ์•Š๊ณ  ๋นˆ ์ฝ”๋“œ์— ์Šคํ”„๋ง ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ๋…ธ์ถœ๋˜์–ด ๊ถŒ์žฅํ•˜์ง€ ์•Š๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. (์ง€์–‘)
์ดˆ๊ธฐํ™”: InitializingBean ์ธํ„ฐํŽ˜์ด์Šค์˜ afterPropertiesSet() ๋ฉ”์†Œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋“œ
์†Œ๋ฉธ: DisposableBean ์ธํ„ฐํŽ˜์ด์Šค์˜ destroy() ๋ฉ”์†Œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋“œ

3๏ธโƒฃ ๋นˆ ์ฝ”๋“œ์— ์Šคํ”„๋ง ์ธํ„ฐํŽ˜์ด์Šค๋Š” ๋…ธ์ถœ๋˜์ง€ ์•Š์ง€๋งŒ, ์ฝ”๋“œ๋งŒ์œผ๋กœ ์ดˆ๊ธฐํ™” ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ์—ฌ๋ถ€๋ฅผ ์•Œ ์ˆ˜ ์—†๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.
์ดˆ๊ธฐํ™”: ์ปค์Šคํ…€ init() ๋ฉ”์†Œ๋“œ ์ •์˜
xml์— init-method ์†์„ฑ์œผ๋กœ ๋ฉ”์†Œ๋“œ ์ด๋ฆ„์„ ์ง€์ •ํ•˜๊ฑฐ๋‚˜, ๋นˆ ์ดˆ๊ธฐํ™” ๋ฉ”์†Œ๋“œ์— @Bean(init-method="init")๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
์†Œ๋ฉธ : ์ปค์Šคํ…€ destroy() ๋ฉ”์†Œ๋“œ ์ •์˜
xml์— destroy-method ์†์„ฑ์œผ๋กœ ๋ฉ”์†Œ๋“œ ์ด๋ฆ„ ์ง€์ •

  • singleton(default): Spring IoC ์ปจํ…Œ์ด๋„ˆ ๋‹น ํ•œ ๊ฐœ์˜ ์ธ์Šคํ„ด์Šค๋งŒ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์œผ๋กœ, ์ปจํ…Œ์ด๋„ˆ๊ฐ€ Bean์„ ์ฃผ์ž…ํ•  ๋•Œ ํ•ญ์ƒ ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ”๋ชจ๋ฆฌ๋‚˜ ์„ฑ๋Šฅ ์ตœ์ ํ™”์— ์œ ๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
  • prototype: ์ปจํ…Œ์ด๋„ˆ๊ฐ€ Bean์„ ์ฃผ์ž…ํ•  ๋•Œ ํ•ญ์ƒ ๋‹ค๋ฅธ ์ธ์Šคํ„ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋“  ์š”์ฒญ์—์„œ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์•ˆ์“ฐ๋Š” ๊ฐ์ฒด๋Š” GC์— ์˜ํ•ด Bean์ด ์ œ๊ฑฐ๋ฉ๋‹ˆ๋‹ค.
  • request: ํ•˜๋‚˜์˜ HTTP Request ์ƒ๋ช… ์ฃผ๊ธฐ ์•ˆ์— ๋‹จ ํ•˜๋‚˜์˜ Bean์ด ์กด์žฌํ•˜๋Š” ๊ฒƒ์œผ๋กœ, ๊ฐ ์š”์ฒญ์€ ๊ณ ์œ  Bean ๊ฐ์ฒด๋ฅผ ๋ณด์œ ํ•ฉ๋‹ˆ๋‹ค. Spring MVC Web Application์—์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • session: ํ•˜๋‚˜์˜ HTTP Session ์•ˆ์— ๋‹จ ํ•˜๋‚˜์˜ Bean์ด ์กด์žฌํ•˜๋Š” ๊ฒƒ์œผ๋กœ, Spring MVC Web Application์—์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • global session: ํ•˜๋‚˜์˜ Global HTTP Session ์ƒ๋ช…์ฃผ๊ธฐ ์•ˆ์— ํ•˜๋‚˜์˜ Bean์„ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์œผ๋กœ, Spring MVC Web Application์—์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • application: ํ•˜๋‚˜์˜ ServletContext ์ƒ๋ช…์ฃผ๊ธฐ ์•ˆ์— ํ•˜๋‚˜์˜ Bean์ด ์กด์žฌํ•˜๋Š” ๊ฒƒ์œผ๋กœ, Spring MVC Web Application์—์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
โœ… @Bean ๊ณผ @Component์˜ ์ฐจ์ด

๐Ÿ’ก @Bean
๋ฉ”์†Œ๋“œ ๋ ˆ๋ฒจ์—์„œ ์„ ์–ธ๋˜๋ฉฐ, ๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ์ฒด(์ธ์Šคํ„ด์Šค)๋ฅผ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ˆ˜๋™์œผ๋กœ ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ’ก @Component
ํด๋ž˜์Šค ๋ ˆ๋ฒจ์—์„œ ์„ ์–ธ๋˜๋ฉฐ, ์Šคํ”„๋ง์ด ๋Ÿฐํƒ€์ž„ ์‹œ์— ์ปดํฌ๋„ŒํŠธ ์Šค์บ”์„ ํ•˜์—ฌ ์ž๋™์œผ๋กœ ๋นˆ์„ ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค.