@Qualifier

Reference: Spring Docs - @Qualifier

@Qualifier ๋ž€?

Spring Framework์—์„œ bean์„ ์ฃผ์ž…ํ•  ๋•Œ ์–ด๋–ค bean์„ ์„ ํƒํ• ์ง€ ๋ช…์‹œ์ ์œผ๋กœ ์ง€์ •ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค

Problem

  • Autowire Need for Disambigution

    • @Autowired annotation ์œผ๋กœ ์˜์กด์„ฑ์„ ์ฃผ์ž…ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๋ช‡๊ฐ€์ง€ ์ฃผ์˜ํ•  ์ ์ด ์žˆ๋‹ค

    • ๋งŒ์•ฝ ๊ฐ™์€ type์„ ๊ฐ€์ง„ ํ•œ๊ฐœ ์ด์ƒ์˜ bean์ด container์— ์กด์žฌํ•œ๋‹ค๋ฉด, ์Šคํ”„๋ง์€ NoUniqueBeanDefinitionException ์„ ๋ฐœ์ƒ์‹œํ‚ฌ ๊ฒƒ์ด๋‹ค

Solution

@Qualifier annotation์„ ์‚ฌ์šฉํ•˜์—ฌ autowire ํ•˜๊ณ ์ž ํ•˜๋Š” ๋Œ€์ƒ์„ ํŠน์ •ํ•  ์ˆ˜ ์žˆ๋‹ค

@Qualifier ์‚ฌ์šฉ ์‹œ ์ฃผ์˜ํ•  ์ 

@Qualifier ์— ์ง€์ •ํ•œ ํ•œ์ •์ž ๊ฐ’์„ ๊ฐ–๋Š” bean ๊ฐ์ฒด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด NoSuchBeanDefinitionException์ด ๋ฐœ์ƒํ•œ๋‹ค

Last updated