没有定义[javax.persistence.EntityManagerFactory]类型的限定bean:期望的单个匹配bean但找到2

时间:2016-06-29 13:26:31

标签: java spring orm annotations

我正在尝试解决两个数据源的问题。我在互联网上阅读了很多文字,并根据this source

制作了简单的解决方案

我已经制作了两个“配置类”,如下所示:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "ResultEntityManagerFactory", 
        transactionManagerRef = "ResultEntityTransactionManager",
        basePackages = "com.migr.result.repositories"
        )
public class ResultTablesConfig {
    @Autowired
    @Qualifier("orclAdapter")
    JpaVendorAdapter jpaVendorAdapter;

    @Bean(name="ResultEntityDataSource")
    @ConfigurationProperties(prefix = "datasource.migr.result")
     public DataSource declReaconDS() {
        return new DataSource();
    }

    @Bean(name = "ResultEntityManager")
    public EntityManager entityManager() {
        return entityManagerFactory().createEntityManager();
    }

    @Bean(name = "ResultEntityManagerFactory")
    public EntityManagerFactory entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
        lef.setDataSource(declReaconDS());
        lef.setJpaVendorAdapter(jpaVendorAdapter);
        lef.setPackagesToScan("com.migr.result.tables.*");
        lef.setPersistenceUnitName("ResultEntityPersistenceUnit");
        lef.afterPropertiesSet();
        return lef.getObject();
    }

    @Bean(name = "ResultEntityTransactionManager")
    public PlatformTransactionManager transactionManager() {
        return new JpaTransactionManager(entityManagerFactory());
    }
}

和另一个,几乎相同 - SourceTablesConfig。 唯一的区别是@Bean名称(它们都以“Source”开头)和存储库映射,如下所示:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "SourceEntityManagerFactory", 
        transactionManagerRef = "SourceEntityTransactionManager",
                basePackages = "com.migr.source.repositories"
            )

public class SourceTablesConfig {
...............
@Bean(name = "SourceEntityManagerFactory")
public EntityManagerFactory entityManagerFactory(){
     LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
     lef.setDataSource(declReaconDS());
     lef.setJpaVendorAdapter(jpaVendorAdapter);
     lef.setPackagesToScan("com.migr.source.tables.*");
     lef.setPersistenceUnitName("SourceEntityPersistenceUnit");
     lef.afterPropertiesSet();
     return lef.getObject();
}

我的服务很简单:

@Service
public class CatalogsSourceService {
@Autowired
CatalogsSourceRepository catalogsrepository;
 .......
 }

其中CatalogsSourceRepository extends JpaRepository<Catalogs, Long>

我的“主要”课程:

@RestController
@RequestMapping("/")
public class Main {

@Autowired
CatalogsSourceService source_serv;

@Autowired
CatalogsResultService result_serv;
 @RequestMapping
public Object index() {
    Map<String,List> results = new HashMap<>();
    results.put("First", source_serv.findAll());
    results.put("Second", source_serv.findAll());
    return results;
}

当我只有一个DataSource时,它工作得很好, 但现在它以错误退出: expected single matching bean but found 2: SourceEntityManagerFactory,ResultEntityManagerFactory

我不明白为什么会这样。我的解决方案看起来几乎就像上面链接中提到的那样,但他没有得到这样的错误。

我将非常感谢任何帮助

PS。忘了我的“AppConfig”:

@Configuration
@ComponentScan(basePackages = { "com.migr" })
public class AppConfig {
    @Bean(name="orclAdapter")
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter jpaVendorAdapter = new     HibernateJpaVendorAdapter();
        jpaVendorAdapter.setShowSql(true);
        jpaVendorAdapter.setDatabase(Database.ORACLE);        
        return jpaVendorAdapter;
    }

    @Bean
    public HibernateExceptionTranslator hibernateExceptionTranslator() {
        return new HibernateExceptionTranslator();
    }
}

1 个答案:

答案 0 :(得分:0)

对不起伙计们,我的问题解决了。我错过了最简单的解决方案)。 我只需要用EntityManagerFactory注释注释一个@Primary bean。

@Bean(name = "SourceEntityManagerFactory")
@Primary
public EntityManagerFactory entityManagerFactory() {
...........
}

明显的解决方案并不总是那么明显......