![](/img/trans.png)
[英]How to restrict to a specific domain to login with Spring-Boot and OAuth2
[英]Spring-Boot: How to restrict the visibility of Beans
我有兩個自定義的PlatformTransactionManager
bean注入Spring框架,具體names
如下:
@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);
}
第三方庫具有@Autowired protected PlatformTransactionManager transactionManager;
依賴。 因此,第三方庫不應該使用兩個TransactionManagers
。 但是,如您所見,外部庫中沒有Qualifier
用於依賴項注入,我收到如下錯誤:
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]
那么,我如何限制兩個Beans
的可見性,以便第三方庫無法訪問它們?
提供DefaultDatasourceConfiguration
以使用默認的Spring bean,例如名為dataSource
DataSource
和名為transcationManager
PlatformTransactionManager
。 將Camunda粘貼到Spring Boot應用程序中是默認的,該應用程序默認具有單個數據源。
由於您已經創建了自己的PlatformTransactionManager
bean,因此禁用了Spring Boot的默認事務管理器bean(名為transcationManager
(根據TransactionAutoConfiguration
Spring Boot自動配置邏輯)。
您很可能需要為Camunda的流程引擎再定義一個transactionManager
(可能還有dataSource
),這需要它自己的架構。 確保使用正確的bean名稱,如下所示:
@Bean
public PlatformTransactionManager transactionManager() {
...
}
從Spring 4開始,bean名稱是自動連接時的默認限定符,因此新事務管理器將連接到DefaultDatasourceConfiguration
因為它匹配類中的字段名稱。
或者,如果Spring Boot默認設置不適合您,請不要使用DefaultDatasourceConfiguration
並推出自己的配置。
使用@Qualifier注釋當有多個相同類型的bean時,@ Qualifier注釋用於解決自動裝配沖突。
@Bean
@Qualifier("ubldbTransactionManager")
protected PlatformTransactionManager transactionManager
和
@Bean
@Qualifier("bpdbTransactionManager")
public PlatformTransactionManager bpdbTransactionManager
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.