@Qualifier not working with multiple DataSource beans in Spring Boot
<p>I'm trying here to setup four MySQL data sources with spring boot application using four JPA entitymanagers. I'm new to spring and spring boot. I googled this to get a clue, but the results were very confusing, there are tons of approaches of configurations(but not for handling multi data sources), I'm not sure what to follow. Eventually I'm trying to setup four separate entity managers for each MySQL data sources and use them separately in transactions. </p>

<p><strong>application.properties</strong></p>

<pre><code># Database Properties
db.driver: com.mysql.jdbc.Driver
db.urlHotelPos: jdbc:mysql://localhost:3306/hotelwebpos
db.urlHotelFinance: jdbc:mysql://localhost:3306/hotelfinance
db.urlHotelInventory: jdbc:mysql://localhost:3306/hotelinventory
db.urlReservation: jdbc:mysql://localhost:3306/reservation
db.username: user
db.password: passwd

# Hibernate Properties
hibernate.dialect: org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql: true
hibernate.hbm2ddl.auto: update
entitymanager.packagesToScan: com.test.poswebservice.model.*
</code></pre>

<p><strong>DBconfigs.java</strong></p>

<pre><code>@Configuration
@EnableTransactionManagement
public class DBconfigs {

    @Bean
    public DataSource dataSourceHotelPos() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getProperty("db.driver"));
        dataSource.setUrl(env.getProperty("db.urlHotelPos"));
        dataSource.setUsername(env.getProperty("db.username"));
        dataSource.setPassword(env.getProperty("db.password"));
        return dataSource;
    }

    @Bean
    public DataSource dataSourceHotelFinance() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getProperty("db.driver"));
        dataSource.setUrl(env.getProperty("db.urlHotelFinance"));
        dataSource.setUsername(env.getProperty("db.username"));
        dataSource.setPassword(env.getProperty("db.password"));
        return dataSource;
    }

    @Bean
    public DataSource dataSourceHotelInventory() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getProperty("db.driver"));
        dataSource.setUrl(env.getProperty("db.urlHotelInventory"));
        dataSource.setUsername(env.getProperty("db.username"));
        dataSource.setPassword(env.getProperty("db.password"));
        return dataSource;
    }

    @Bean
    public DataSource dataSourceReservation() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getProperty("db.driver"));
        dataSource.setUrl(env.getProperty("db.urlReservation"));
        dataSource.setUsername(env.getProperty("db.username"));
        dataSource.setPassword(env.getProperty("db.password"));
        return dataSource;
    }

    @Bean
    @Qualifier("dataSourceHotelPos")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryHotelPos() {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();

        entityManagerFactory.setDataSource(dataSourceHotelPos());

        entityManagerFactory.setPackagesToScan(env.getProperty("entitymanager.packagesToScan"));

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        entityManagerFactory.setJpaVendorAdapter(vendorAdapter);

        Properties additionalProperties = new Properties();
        additionalProperties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
        additionalProperties.put("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
        additionalProperties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
        entityManagerFactory.setJpaProperties(additionalProperties);

        return entityManagerFactory;
    }

    @Bean
    @Qualifier("dataSourceHotelFinance")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryHotelFinance() {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();

        entityManagerFactory.setDataSource(dataSourceHotelFinance());

        entityManagerFactory.setPackagesToScan(env.getProperty("entitymanager.packagesToScan"));

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        entityManagerFactory.setJpaVendorAdapter(vendorAdapter);

        Properties additionalProperties = new Properties();
        additionalProperties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
        additionalProperties.put("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
        additionalProperties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
        entityManagerFactory.setJpaProperties(additionalProperties);

        return entityManagerFactory;
    }

    @Bean
    @Qualifier("dataSourceHotelInventory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryHotelInventory(){
        LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();

        entityManagerFactory.setDataSource(dataSourceHotelInventory());

        entityManagerFactory.setPackagesToScan(env.getProperty("entitymanager.packagesToScan"));

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        entityManagerFactory.setJpaVendorAdapter(vendorAdapter);

        Properties additionalProperties = new Properties();
        additionalProperties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
        additionalProperties.put("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
        additionalProperties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
        entityManagerFactory.setJpaProperties(additionalProperties);

        return entityManagerFactory;
    }

    @Bean
    @Qualifier("dataSourceReservation")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryReservation() {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();

        entityManagerFactory.setDataSource(dataSourceReservation());

        entityManagerFactory.setPackagesToScan(env.getProperty("entitymanager.packagesToScan"));

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        entityManagerFactory.setJpaVendorAdapter(vendorAdapter);

        Properties additionalProperties = new Properties();
        additionalProperties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
        additionalProperties.put("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
        additionalProperties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
        entityManagerFactory.setJpaProperties(additionalProperties);

        return entityManagerFactory;
    }

    @Bean
    @Qualifier("entityManagerFactoryHotelPos")
    public JpaTransactionManager transactionManagerHotelPos() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactoryHotelPos().getObject());
        return transactionManager;
    }

    @Bean
    @Qualifier("entityManagerFactoryHotelFinance")
    public JpaTransactionManager transactionManagerHotelFinance() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactoryHotelFinance().getObject());
        return transactionManager;
    }

    @Bean
    @Qualifier("entityManagerFactoryHotelInventory")
    public JpaTransactionManager transactionManagerHotelInventory() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactoryHotelInventory().getObject());
        return transactionManager;
    }

    @Bean
    @Qualifier("entityManagerFactoryReservation")
    public JpaTransactionManager transactionManagerReservation() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactoryReservation().getObject());
        return transactionManager;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

    @Autowired
    private Environment env;
}
</code></pre>

<p><strong>Output >></strong></p>

<pre><code>***************************
APPLICATION FAILED TO START
***************************

Description:

Method entityManagerFactoryHotelPos in com.test.poswebservice.configs.DBconfigs required a single bean, but 4 were found:
    - dataSourceHotelPos: defined by method 'dataSourceHotelPos' in class path resource [com/test/poswebservice/configs/DBconfigs.class]
    - dataSourceHotelFinance: defined by method 'dataSourceHotelFinance' in class path resource [com/test/poswebservice/configs/DBconfigs.class]
    - dataSourceHotelInventory: defined by method 'dataSourceHotelInventory' in class path resource [com/test/poswebservice/configs/DBconfigs.class]
    - dataSourceReservation: defined by method 'dataSourceReservation' in class path resource [com/test/poswebservice/configs/DBconfigs.class]


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
</code></pre>

<p>The @Qualifier annotation should avoid this issue, but here it's not working somehow. What's I'm doing wrong here? Please someone help me out, this is almost killing me :-( Any help would be highly appreciated.  </p>

<p>Thanks.</p>