๐ด The Error You're Seeing
Confirm this matches your console output. If it does, you're in the right place.
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.devinhyderabad.repo.UserRepository' available: expected single matching bean but found 2:
- jpaUserRepository
- mongoUserRepositoryโก Quick Fix Works 80% of the time
Use @Qualifier to specify which bean name to inject.
@Autowired
@Qualifier("jpaUserRepository")
private UserRepository repo;๐ง Why this Happens
Tap to expand the deep technical explanation
Spring's application context contains two beans that satisfy the requested type. Because the injection point expects exactly one bean, Spring throws NoUniqueBeanDefinitionException. This is common when integrating multiple database technologies (like JPA and MongoDB) with similar repository names.
The HITEC City Parking Spot Analogy:
A waiter asks the kitchen for 'the soup'. The kitchen has Tomato and Chicken Noodle. The waiter can't guess which one you want, so the order is rejected.
๐ How to Reproduce Confirm this is your error
Create two Spring beans of the exact same type (e.g., two `DataSource` beans, or two interfaces extending `UserRepository`). Try to `@Autowired` that type without specifying a qualifier.
๐ ๏ธ Solutions (5 Ways to Fix)
Use @Qualifier to specify the bean name
๐ Use this when you need both beans but want to inject a specific one here.
Spring will bypass the default matching and inject the exact bean whose name matches the @Qualifier string.
@Service
public class UserService {
@Autowired
@Qualifier("jpaUserRepository")
private UserRepository userRepository;
}Annotate one bean with @Primary
๐ Use this when you want one bean to be the global default.
If multiple beans match, Spring will pick the @Primary one without needing @Qualifier.
@Configuration
public class DataSourceConfig {
@Bean
@Primary
public DataSource mySqlDataSource() { ... }
@Bean
public DataSource postgreDataSource() { ... }
}Rename the bean method to avoid conflicts
๐ Use this if the conflict is caused by identical method names in @Configuration classes.
By default, the bean name is the method name. Renaming makes them unique.
@Bean
public DataSource primaryDataSource() { ... }
@Bean
public DataSource secondaryDataSource() { ... }Remove one of the beans if redundant
๐ Use this if one bean was created accidentally (e.g., a leftover mock).
Delete the @Bean method or @Service class that is causing the conflict.
// Remove this redundant configuration
/*
@Bean
public DataSource secondaryDataSource() { ... }
*/Inject a List of beans instead of a single bean
๐ Use this if you want to use all implementations dynamically (e.g., plugin pattern).
Spring can inject all beans of a type into a List. You can then iterate over them.
@Service
public class UserService {
@Autowired
private List<UserRepository> repositories;
}๐ Version Notes
Standard NoUniqueBeanDefinitionException.
Same exception, but Spring Boot's FailureAnalyzer provides suggestions.
๐ก๏ธ How to Prevent This Next Time
When defining multiple beans of the same type (like DataSources), always configure a @Primary default.