interface 에 λŒ€ν•˜μ—¬ multiple ν•œ implementation 이 μžˆμ„ λ•Œ, μ•žμ—μ„œ @Qualifier annotation 을 μ΄μš©ν•˜μ—¬ ν•΄κ²°ν–ˆλ‹€. κ·ΈλŸ¬λ‚˜ @Primary λΌλŠ” annotation 을 μ‚¬μš©ν•΄μ„œλ„ 해결이 κ°€λŠ₯ν•˜λ‹€.

@Primary annotation 은 multiple implementation κ°€μš΄λ° 단 ν•˜λ‚˜λ§Œ μ‚¬μš©ν•  수 있으며, 마치 default Bean 으둜써 κΈ°λŠ₯ν•œλ‹€. @Qualifier λ₯Ό μ‚¬μš©ν•˜μ—¬ νŠΉμ • Bean object λ₯Ό μ§€μ •ν•˜μ§€ μ•Šλ”λΌλ„ Primary 둜 μ§€μ •λœ Bean 이 Injection λœλ‹€.

κ·ΈλŸ¬λ‚˜ λ§Œμ•½ @Qualifier 와 @Primary λ₯Ό λ‘˜ λ‹€ μ‚¬μš©ν•˜κ²Œ 될 경우, @Qualifier κ°€ 더 높은 priority λ₯Ό κ°€μ§€κΈ° λ•Œλ¬Έμ— @Qualifier λ§Œμ„ λ‹¨λ…μ μœΌλ‘œ μ‚¬μš©ν•˜λŠ” 것을 μΆ”μ²œν•œλ‹€. λ˜ν•œ multiple Primary μ—­μ‹œ 였λ₯˜λ₯Ό λ°œμƒμ‹œν‚¨λ‹€.

Primary - Code

μš°μ„  Primary 둜 μ§€μ •ν•˜κ³  싢은 class 에 λŒ€ν•˜μ—¬ @Primary annotation 을 μ‚¬μš©ν•΄μ£Όλ©΄ λœλ‹€.

@Component  
@Primary  
public class FootballCoach implements Coach { ... }

그리고 Controller 의 Constructor Injection μ—μ„œλŠ” μ•„λž˜μ²˜λŸΌ implementation 이 ν•˜λ‚˜λ°–μ— μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” κ²ƒμ²˜λŸΌ μ‚¬μš©ν•΄μ£Όλ©΄ λœλ‹€.

// define a constructor for dependency injection  
@Autowired  
public DemoController(Coach theCoach) {  
    myCoach = theCoach;  
}