NoUniqueBeanDefinitionException while Autowiring
@Autowired annotation is used to inject a dependency in Spring explicitly. By default, Spring resolves autowired entries by type. In some cases this annotation alone isn't enough for Spring to understand which bean to inject.
If more than one bean of the same type is available in the container, the framework will throw NoUniqueBeanDefinitionException, indicating that more than one bean is available for autowiring.
In the above code, the Spring framework will throw a NoUniqueBeanDefinitionException while trying to load MobileService into our context. Because the Spring doesn't know which bean to inject it throws an exception. We can resolve this by using @Qualifier.
When we need @Qualifier?
Sometimes we need to create more than one bean of the same type and want to wire only one of them with a property. To do this we need to use the @Qualifier annotation along with @Autowired, this will remove the confusion by specifying which exact bean will be wired. e.g:
@Component
public class MobileService {
@Autowired
@Qualifier("iosMobile")
private Mobile mobile;
}
While using @Qualifier, we need to make sure that the qualifier name to be used is the one declared in the @Component annotation (in my case it was 'iosMobile').
FYI, another way to decide between multiple beans when autowiring is by using the name of the field to inject.
What is the difference between @Qualifier and @Primary?
@Primary can use to decide which bean to inject when ambiguity is present regarding dependency injection.
In the below example, both the methods are returning the same Mobile type. Because of @Primary annotation the bean that Spring will inject is the one returned by the method androidMobile().
@Primary annotation is useful when we want to specify which bean of a certain type should be injected by default.
In short, @Primary defines a default, while @Qualifier is very specific.
FYI, if both the @Qualifier and @Primary annotations are present, then the @Qualifier annotation will have precedence.
-K Himaanshu Shuklaa..
@Autowired annotation is used to inject a dependency in Spring explicitly. By default, Spring resolves autowired entries by type. In some cases this annotation alone isn't enough for Spring to understand which bean to inject.
If more than one bean of the same type is available in the container, the framework will throw NoUniqueBeanDefinitionException, indicating that more than one bean is available for autowiring.
In the above code, the Spring framework will throw a NoUniqueBeanDefinitionException while trying to load MobileService into our context. Because the Spring doesn't know which bean to inject it throws an exception. We can resolve this by using @Qualifier.
When we need @Qualifier?
Sometimes we need to create more than one bean of the same type and want to wire only one of them with a property. To do this we need to use the @Qualifier annotation along with @Autowired, this will remove the confusion by specifying which exact bean will be wired. e.g:
@Component
public class MobileService {
@Autowired
@Qualifier("iosMobile")
private Mobile mobile;
}
While using @Qualifier, we need to make sure that the qualifier name to be used is the one declared in the @Component annotation (in my case it was 'iosMobile').
FYI, another way to decide between multiple beans when autowiring is by using the name of the field to inject.
What is the difference between @Qualifier and @Primary?
@Primary can use to decide which bean to inject when ambiguity is present regarding dependency injection.
In the below example, both the methods are returning the same Mobile type. Because of @Primary annotation the bean that Spring will inject is the one returned by the method androidMobile().
@Primary annotation is useful when we want to specify which bean of a certain type should be injected by default.
In short, @Primary defines a default, while @Qualifier is very specific.
FYI, if both the @Qualifier and @Primary annotations are present, then the @Qualifier annotation will have precedence.
-K Himaanshu Shuklaa..
No comments:
Post a Comment