🔴 The Error You're Seeing

Confirm this matches your console output. If it does, you're in the right place.

ERROR LOGorg.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)

Solution 1✓ Most common cause

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; }
Solution 2

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() { ... } }
Solution 3

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() { ... }
Solution 4

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() { ... } */
Solution 5

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

Spring Boot 2.x

Standard NoUniqueBeanDefinitionException.

Spring Boot 3.x

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.