2016-09-29 75 views
2

我在這裏試圖使用四個JPA entitymanagers來設置四個MySQL數據源,並使用spring啓動應用程序。我是春季和春季新手的新手。我搜索了這個以獲得線索,但結果非常混亂,有大量的配置方法(但不能處理多個數據源),我不知道要遵循什麼。最終,我試圖爲每個MySQL數據源設置四個獨立的實體管理器,並在事務中單獨使用它們。@Qualifier不能在Spring Boot中使用多個DataSource beans

application.properties

# 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.* 

DBconfigs.java

@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; 
} 

輸出>>

*************************** 
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 

的@Qualifier批註應該避免這個問題,但這裏它不能以某種方式工作。我在這裏做錯了什麼?請人幫我,這幾乎殺了我:-(任何幫助將高度讚賞

感謝

回答

0

在我的設置是這樣工作是:。

  1. 從取出@Qualifier您的EMF豆
  2. 添加在每個EMF豆:@DependsOn({"dataSource"})用正確的DataSource
  3. 的確使DataSource小號@Primary
  4. 012之一
+0

爲什麼我們應該在這裏創建一個數據源bean'@ Primary'(P.S。即使在我之前的設置中,當生成數據源bean「@ primary」時,錯誤消失了,我想它對每個EMF bean都使用相同的主數據源,我將再次確認再次檢查。)使用「@ primary」表示單個EMF bean將接受多個數據源bean,對吧?一個是主要的,另一個是可選的?但在我的情況下,我只想讓EMF bean使用一個特定的數據源bean。我是否搞錯了什麼?預先感謝提供的線索。 – wannix

+1

我可以確認這個設置不能像預期的那樣工作。 EMF bean始終選擇'@ Primary'數據源。 '@ DependsOn'完全沒有效果。 '@ Primary'是獨裁者。 – wannix

相關問題