title: mybatis mulidatasource in springboot with the problem 'more than one 'primary' bean found among candidates'
body: <p>This problem that 'more than one "primary" bean found among candidates' always occurs when I configure mybatis multiple data sources in springboot.</p>
  
  <p><strong>the master data sources is configured as follows：</strong></p>
  
  <pre><code>@Configuration
  @MapperScan(basePackages = "com.monkey.myproject.dao.first", sqlSessionTemplateRef = "sqlSessionTemplate1")
  public class DataSourceConfig1 {
      @Bean(name = "dataSource1")
      @ConfigurationProperties(prefix = "spring.datasource.first")
      @Primary
      public DataSource dataSource1() {
          return DataSourceBuilder.create().build();
      }
  
      @Bean(name = "sqlSessionFactory1")
      @Primary
      public SqlSessionFactory sqlSessionFactory1(@Qualifier("dataSource1") DataSource dataSource1) throws Exception {
          SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
          bean.setDataSource(dataSource1);
          bean.setMapperLocations(
                  new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/first/*.xml"));
          return bean.getObject();
      }
  
      @Bean(name = "transactionManager1")
      @Primary
      public DataSourceTransactionManager transactionManager1(@Qualifier("dataSource1") DataSource dataSource1) {
          return new DataSourceTransactionManager(dataSource1);
      }
  
      @Bean(name = "sqlSessionTemplate1")
      @Primary
      public SqlSessionTemplate sqlSessionTemplate1(@Qualifier("sqlSessionFactory1") SqlSessionFactory sqlSessionFactory1)
              throws Exception {
          return new SqlSessionTemplate(sqlSessionFactory1);
      }
  }
  </code></pre>
  
  <p><strong>the slave data sources is configured as follows：</strong></p>
  
  <pre><code>@Configuration
  @MapperScan(basePackages = "com.monkey.myproject.dao.second", sqlSessionTemplateRef = "sqlSessionTemplate2")
  public class DataSourceConfig2 {
      @Bean(name = "dataSource2")
      @ConfigurationProperties(prefix = "spring.datasource.second")
      public DataSource dataSource2() {
          return DataSourceBuilder.create().build();
      }
  
      @Bean(name = "sqlSessionFactory2")
      public SqlSessionFactory sqlSessionFactory2(@Qualifier("dataSource2") DataSource dataSource2) throws Exception {
          SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
          bean.setDataSource(dataSource2);
          bean.setMapperLocations(
                  new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/second/*.xml"));
          return bean.getObject();
      }
  
      @Bean(name = "transactionManager2")
      public DataSourceTransactionManager transactionManager2(@Qualifier("dataSource2") DataSource dataSource2) {
          return new DataSourceTransactionManager(dataSource2);
      }
  
      @Bean(name = "sqlSessionTemplate2")
      public SqlSessionTemplate sqlSessionTemplate2(@Qualifier("sqlSessionFactory2") SqlSessionFactory sqlSessionFactory2)
              throws Exception {
          return new SqlSessionTemplate(sqlSessionFactory2);
      }
  }
  </code></pre>
  
  <p><strong>The Junit Test is as follows：</strong></p>
  
  <pre><code>@Autowired
  @Qualifier("sqlSessionTemplate1")
  private SqlSessionTemplate sqlSessionTemplate1;
  @Test
  public void test2() {
      try {
          sqlSessionTemplate1.insert("FirstMapper.insertOne", null);
      } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>When this test is executed, the exception message appears,as follows：</p>
  
  <pre><code>more than one 'primary' bean found among candidates: [primaryDataSource, secondaryDataSource, dataSource1, dataSource2]
  </code></pre>
  
  <p>note:When I remove this annotation '@Primary' from master,This test case passes.but that is why?please help me,thank you in advance.</p>