title: Differences between @MapperScan and @Bean MapperScannerConfigurer
body: <p>I have a multi-datasource web application with following technique:</p>
  
  <ul>
  <li>Spring boot 1.5.12</li>
  <li>Mybats-Spring-boot-starter 1.3.2</li>
  </ul>
  
  <p>And I prefered Java based configuration. Therefore, I have Datasource1Config.java and Datasource2Config.java.</p>
  
  <p>I defined SqlSessionTemplate respectively, and using MapperScannerConfigure to inject my mapper. Following is for datasource1, and the datasource2 just substitute the number.</p>
  
  <pre><code>@Bean(name = "dataSource1MapperScannerConfigurer")
  public MapperScannerConfigurer msc() {
      MapperScannerConfigurer msc = new MapperScannerConfigurer();
      msc.setSqlSessionFactoryBeanName("dataSource1SqlSessionFactory");
      msc.setSqlSessionTemplateBeanName("dataSource1SqlSessionFactory");
      msc.setBasePackage("demo.mybatisspring.mapper.ds1");
      return msc;
  }
  </code></pre>
  
  <p>And then, the error happend </p>
  
  <pre><code>file [C:\...target\classes\demo\mybatisspring\mapper\ds1\UserMapper.class] required a single bean, but 2 were found:
  - dataSource1SqlSessionFactory: defined by method 'sqlSessionFactoryBean' in class path resource [demo/mybatisspring/config/DataSource1Config.class]
  - dataSource2SqlSessionFactory: defined by method 'sqlSessionFactoryBean2' in class path resource [demo/mybatisspring/config/DataSource2Config.class]
  </code></pre>
  
  <p>However, if I inject mappers with @MapperScan as following, everything will work fine. (also worked fine when one using @MapperScan and the other using @Bean MapperScannerConfigurer)</p>
  
  <pre><code>@MapperScan(basePackages = "demo.mybatisspring.mapper.ds1", sqlSessionTemplateRef = "dataSource1SqlSessionFactory")
  public class DataSource1Config {...}
  
  @MapperScan(basePackages = "demo.mybatisspring.mapper.ds2", sqlSessionTemplateRef = "dataSource2SqlSessionFactory")
  public class DataSource2Config {...}
  </code></pre>
  
  <p>I've tried to trace with debug mode and search so many articles on internet, still can not get the answer instead. So if anyone can help me? </p>
  
  <p>Thanks for your time.</p>