[英]spring-data - multiple DB: NoUniqueBeanDefinitionException No qualifying bean of type 'TransactionManager' available
I have created a spring-data project (version = 2.5.5) with two datasources GitHub .我创建了一个带有两个数据源GitHub的 spring-data 项目(版本 = 2.5.5)。 It works fine, as long, as I am using the interface
CrudRepository.java
.只要我使用接口
CrudRepository.java
。 But when I am trying to implement my own DAO based on SimpleJpaRepository.java
I am getting the error但是当我尝试基于
SimpleJpaRepository.java
实现我自己的 DAO 时,我收到了错误
No qualifying bean of type 'org.springframework.transaction.TransactionManager' available: expected single matching bean but found 2: db1TransactionManager,db2TransactionManager
DB2Config.java (DB1Config.java is the same but instead of '2' the bean names has '1') DB2Config.java (DB1Config.java 是相同的,但 bean 名称不是 '2',而是 '1')
@Configuration
@PropertySource("classpath:db2-config.properties")
@EnableJpaRepositories(
basePackages = "com.vscoding.jpa.db2.entity",
entityManagerFactoryRef = "db2EntityManagerFactory",
transactionManagerRef = "db2TransactionManager"
)
public class DB2Config {
@Bean
@ConfigurationProperties(prefix = "db2.datasource")
public DataSource db2DataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public FactoryBean<EntityManagerFactory> db2EntityManagerFactory(@Qualifier("db2DataSource") DataSource db2DataSource, Environment env){
var em = new LocalContainerEntityManagerFactoryBean();
var va = new HibernateJpaVendorAdapter();
var properties = new HashMap<String,Object>();
properties.put("hibernate.hbm2ddl.auto","create");
em.setDataSource(db2DataSource);
em.setPackagesToScan("com.vscoding.jpa.db2.entity");
em.setJpaVendorAdapter(va);
em.setJpaPropertyMap(properties);
return em;
}
@Bean
public PlatformTransactionManager db2TransactionManager(@Qualifier("db2EntityManagerFactory") FactoryBean<EntityManagerFactory> db2EntityManagerFactory) throws Exception {
var tm = new JpaTransactionManager();
tm.setEntityManagerFactory(db2EntityManagerFactory.getObject());
return tm;
}
@Bean
@Profile("with-init")
public DataSourceInitializer dataSourceInitializer2(@Qualifier("db2DataSource") DataSource datasource) {
var populator = new ResourceDatabasePopulator();
populator.addScript(new ClassPathResource("db2.sql"));
DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
dataSourceInitializer.setDataSource(datasource);
dataSourceInitializer.setDatabasePopulator(populator);
return dataSourceInitializer;
}
}
ProductCustomRepository.java ProductCustomRepository.java
@Repository
@Transactional(transactionManager = "db2TransactionManager")
public class ProductCustomRepository extends SimpleJpaRepository<ProductEntity2, Integer> {
private final EntityManager entityManager;
public ProductCustomRepository(@Qualifier("db2EntityManagerFactory") EntityManager entityManager) {
super(ProductEntity2.class, entityManager);
this.entityManager = entityManager;
}
public List<ProductEntity2> customQuery() {
var query = entityManager.createQuery("SELECT p FROM ProductEntity2 p WHERE p.name='special'",ProductEntity2.class);
return query.getResultList();
}
}
I would expect, that @Transactional(transactionManager = "db2TransactionManager")
would select the right transactionManager
, but maybe I am missing something.我希望
@Transactional(transactionManager = "db2TransactionManager")
会选择正确的transactionManager
,但也许我错过了一些东西。
ProductCustomRepositoryTest.java (test to reproduce the error) ProductCustomRepositoryTest.java (测试重现错误)
@SpringBootTest
class ProductCustomRepositoryTest {
@Autowired
private ProductCustomRepository sut;
@Test
void customQuery() {
//Given
var special = new ProductEntity2("special");
sut.save(special);
//When
var result = sut.customQuery();
//Then
assertTrue(result.contains(special));
}
}
Thanks in advance.提前致谢。
As mentioned by Simon, I could solve the error, by following https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.custom-implementations正如西蒙提到的,我可以通过遵循https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.custom-implementations来解决错误
Changed my ProductCustomRepository.java
to CustomRepositoryImpl.java
and added Interface CustomRepository.java
将我的
ProductCustomRepository.java
更改为CustomRepositoryImpl.java
并添加了接口CustomRepository.java
@Repository
@Transactional(transactionManager = "db2TransactionManager")
public class CustomRepositoryImpl implements CustomRepository {
// no changes here
}
Then extend my main Repository with the interface.然后使用界面扩展我的主存储库。
public interface ProductRepository2 extends CrudRepository<ProductEntity2,Integer>,CustomRepository {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.