使用MyBatis的Spring:期望的单个匹配bean但找到2 [英] Spring with MyBatis: expected single matching bean but found 2

查看:935
本文介绍了使用MyBatis的Spring:期望的单个匹配bean但找到2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Spring和MyBatis,它对单个数据库的效果非常好。我在尝试添加另一个数据库时遇到了困难(参见 Github上可重复的示例

I've been using Spring with MyBatis and it's been working really well for a single database. I ran into difficulties when trying to add another database (see reproducible example on Github).

我正在使用Spring Java配置(即不是XML)。我见过的大多数示例都展示了如何使用XML实现这一点。

I'm using Spring Java configuration (i.e. not XML). Most of the examples I've seen show how to achieve this using XML.

我有两个数据配置类(A& B),如下所示:

I have two data configuration classes (A & B) like this:

@Configuration
@MapperScan("io.woolford.database.mapper")
public class DataConfigDatabaseA {

    @Bean(name="dataSourceA")
    public DataSource dataSourceA() throws SQLException {
        SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
        dataSource.setDriver(new com.mysql.jdbc.Driver());
        dataSource.setUrl("jdbc:mysql://" + dbHostA + "/" + dbDatabaseA);
        dataSource.setUsername(dbUserA);
        dataSource.setPassword(dbPasswordA);
        return dataSource;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSourceA());
        return sessionFactory.getObject();
    }
}

两个映射器和一个自动装配映射器的服务:

Two mappers, and a service that autowires the mappers:

@Service
public class DbService {

    @Autowired
    private DbMapperA dbMapperA;

    @Autowired
    private DbMapperB dbMapperB;

    public List<Record> getDabaseARecords(){
        return dbMapperA.getDatabaseARecords();
    }

    public List<Record> getDabaseBRecords(){
        return dbMapperB.getDatabaseBRecords();
    }

}

应用程序无法启动:

Error creating bean with name 'dataSourceInitializer': 
  Invocation of init method failed; nested exception is 
    org.springframework.beans.factory.NoUniqueBeanDefinitionException: 
      No qualifying bean of type [javax.sql.DataSource] is defined: 
        expected single matching bean but found 2: dataSourceB,dataSourceA

我读过可以使用 @Qualifier 注释消除了自动装配的歧义,但我不知道在哪里添加它。

I've read that it's possible to use the @Qualifier annotation to disambiguate the autowiring, though I wasn't sure where to add it.

你能看到我哪里出错吗?

Can you see where I'm going wrong?

推荐答案

最后,我们将每个映射器放在自己的文件夹中:

In the end, we put each mapper in its own folder:

src/main/java/io/woolford/database/mapper/a/DbMapperA.java
src/main/java/io/woolford/database/mapper/c/DbMapperB.java

然后我们创建了两个 DataConfig 类,每个数据库一个。 @MapperScan 注释解析了预期的单个匹配bean,但发现了2 问题。

We then created two DataConfig classes, one for each database. The @MapperScan annotation resolved the expected single matching bean but found 2 issue.

@Configuration
@MapperScan(value = {"io.woolford.database.mapper.a"}, sqlSessionFactoryRef="sqlSessionFactoryA")
public class DataConfigDatabaseA {

有必要添加 @Primary 对其中一个 DataConfig 类中的bean的注释:

It was necessary to add the @Primary annotation to the beans in one of the DataConfig classes:

@Bean(name="dataSourceA")
@Primary
public DataSource dataSourceA() throws SQLException {
    ...
}

@Bean(name="sqlSessionFactoryA")
@Primary
public SqlSessionFactory sqlSessionFactoryA() throws Exception {
    ...
}

感谢所有帮助过的人。毫无疑问,这样做的方法不止一种。我按照@eduardlofitskyi和@的推荐,尝试了 @Qualifier @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class}) GeminiKeith,但是这会产生一些进一步的错误。

Thanks to everyone who helped. No doubt, there's more than one way to do this. I did try @Qualifier and @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class}) as recommended by @eduardlofitskyi and @GeminiKeith, but that generated some further errors.

如果它有用,那么适用于我们的解决方案会发布在这里: https://github.com/alexwoolford/mybatis-spring-multiple-mysql-reproducible-example

In case it's useful, the solution that worked for us is posted here: https://github.com/alexwoolford/mybatis-spring-multiple-mysql-reproducible-example

这篇关于使用MyBatis的Spring:期望的单个匹配bean但找到2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆