[英]No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2
I'm trying to solve a problem with two datasources. 我正在尝试解决两个数据源的问题。 I read many text in the internet and made simple solution based on this source
我在互联网上阅读了许多文字,并根据此来源提出了简单的解决方案
I've made two "config classes", like this: 我做了两个“配置类”,像这样:
@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());
}
}
and another one, practically same - SourceTablesConfig
. 和另一个几乎相同的
SourceTablesConfig
。 only difference is @Bean
names (they all start with "Source") and repositories mapping, like this: 唯一的区别是
@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();
}
My services are simle: 我的服务很简单:
@Service
public class CatalogsSourceService {
@Autowired
CatalogsSourceRepository catalogsrepository;
.......
}
where CatalogsSourceRepository extends JpaRepository<Catalogs, Long>
CatalogsSourceRepository extends JpaRepository<Catalogs, Long>
My "main" class: 我的“主要”课程:
@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;
}
it worked perfectly when I was having only one DataSource, but now it exits with error: expected single matching bean but found 2: SourceEntityManagerFactory,ResultEntityManagerFactory
当我只有一个DataSource时,它工作得很好,但是现在它退出并出现错误:
expected single matching bean but found 2: SourceEntityManagerFactory,ResultEntityManagerFactory
I don't get why it is happening. 我不知道为什么会这样。 My solution looks practically like mentioned in above link, but his do not get such error.
我的解决方案实际上就像上面链接中提到的那样,但是他没有得到这样的错误。
I will be very gratefull for any help 我将非常感谢您的帮助
PS. PS。 forgot about my "AppConfig":
忘记了我的“ 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();
}
}
Sorry guys, my problem is solved. 抱歉,我的问题解决了。 I missed easiest solution).
我错过了最简单的解决方案)。 I just had to annotate one
EntityManagerFactory
bean with @Primary
annotation. 我只需要用
@Primary
注释来注释一个EntityManagerFactory
bean。
@Bean(name = "SourceEntityManagerFactory")
@Primary
public EntityManagerFactory entityManagerFactory() {
...........
}
obvious solutions are not always so obvious... 显而易见的解决方案并不总是那么明显...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.