I have two custom PlatformTransactionManager
beans injected into the Spring framework with specific names
as follows:
@Bean(name = "ubldbTransactionManager")
protected PlatformTransactionManager transactionManager(
@Qualifier("ubldbEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
@Bean(name = "bpdbTransactionManager")
public PlatformTransactionManager bpdbTransactionManager(
@Qualifier("bpdbEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
A 3rd-party library has a @Autowired protected PlatformTransactionManager transactionManager;
dependency. So, the 3rd party library is not supposed to use none of the two TransactionManagers
. However, as you see there is no Qualifier
for the dependency injection in the external library and I get an error as follows:
Field transactionManager in org.camunda.bpm.spring.boot.starter.configuration.impl.DefaultDatasourceConfiguration required a single bean, but 2 were found:
- bpdbTransactionManager: defined by method 'bpdbTransactionManager' in class path resource [eu/nimble/service/bp/config/BusinessProcessDBConfig.class]
- ubldbTransactionManager: defined by method 'transactionManager' in class path resource [eu/nimble/service/bp/config/UBLDBConfig.class]
So, how can I restrict the visibility of the two Beans
so that they would not be accessible by the 3rd-party library?
DefaultDatasourceConfiguration
is provided to use default Spring beans eg DataSource
named dataSource
and PlatformTransactionManager
named transcationManager
. It's there to glue Camunda into a Spring Boot application which be default has a single data source.
Since you have created your own PlatformTransactionManager
beans this disabled Spring Boot's default transaction manager bean named transcationManager
(as per TransactionAutoConfiguration
Spring Boot auto-configuration logic).
You most likely need to define one more transactionManager
(and potentially dataSource
) for Camunda's process engine, which requires it's own schema. Make sure to use the right bean name as below:
@Bean
public PlatformTransactionManager transactionManager() {
...
}
Starting from Spring 4 the bean name is the default qualifier when auto-wiring so the new transaction manager will be wired into DefaultDatasourceConfiguration
as it matches the field name in the class.
Alternatively don't use DefaultDatasourceConfiguration
and roll out your own configuration if Spring Boot defaults are not working for you.
Use @Qualifier annotation The @Qualifier annotation is used to resolve the autowiring conflict, when there are multiple beans of same type.
@Bean
@Qualifier("ubldbTransactionManager")
protected PlatformTransactionManager transactionManager
and
@Bean
@Qualifier("bpdbTransactionManager")
public PlatformTransactionManager bpdbTransactionManager
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.