Skip to content

Latest commit

ย 

History

History
42 lines (28 loc) ยท 1.13 KB

Component_vs_Bean.md

File metadata and controls

42 lines (28 loc) ยท 1.13 KB

๐Ÿฑโ€๐Ÿ‘ค Component vs Bean

Component

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
public @interface Component {

	...

}
  • @Component๋Š” Class์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ด๋‹ค.
  • ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•˜๊ณ  ์ด๋ฅผ Bean์œผ๋กœ ๋“ฑ๋กํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
  • ๋”ฐ๋ผ์„œ, @Component๋Š” ํ•ด๋‹น ํด๋ž˜์Šค๋ฅผ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๊ณ  ์ด๋กœ๋ถ€ํ„ฐ ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค๋ฅผ IoC Container์— ๋‹ด๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

Bean

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Bean {

	...

}
  • @Bean์€ Method์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ด๋‹ค.
  • ์ •ํ™•ํžˆ๋Š” ํŠน์ • ๊ฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ์— ์‚ฌ์šฉํ•œ๋‹ค. ์ด ์ธ์Šคํ„ด์Šค๊ฐ€ Bean์œผ๋กœ ๋“ฑ๋ก๋œ๋‹ค.
  • ๋”ฐ๋ผ์„œ, @Bean์€ ํ•ด๋‹น ํด๋ž˜์Šค๋ฅผ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ œ์–ดํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ(ex: ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ) ์ด ์ธ์Šคํ„ด์Šค๋ฅผ IoC Container์— ๋‹ด๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

Reference

๊ธฐ์–ต๋ณด๋‹จ ๊ธฐ๋ก์„ - @Bean vs @Component