简体   繁体   English

Spring-Boot:如何限制Beans的可见性

[英]Spring-Boot: How to restrict the visibility of Beans

I have two custom PlatformTransactionManager beans injected into the Spring framework with specific names as follows: 我有两个自定义的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);
}

A 3rd-party library has a @Autowired protected PlatformTransactionManager transactionManager; 第三方库具有@Autowired protected PlatformTransactionManager transactionManager; dependency. 依赖。 So, the 3rd party library is not supposed to use none of the two TransactionManagers . 因此,第三方库不应该使用两个TransactionManagers However, as you see there is no Qualifier for the dependency injection in the external library and I get an error as follows: 但是,如您所见,外部库中没有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]

So, how can I restrict the visibility of the two Beans so that they would not be accessible by the 3rd-party library? 那么,我如何限制两个Beans的可见性,以便第三方库无法访问它们?

DefaultDatasourceConfiguration is provided to use default Spring beans eg DataSource named dataSource and PlatformTransactionManager named transcationManager . 提供DefaultDatasourceConfiguration以使用默认的Spring bean,例如名为dataSource DataSource和名为transcationManager PlatformTransactionManager It's there to glue Camunda into a Spring Boot application which be default has a single data source. 将Camunda粘贴到Spring Boot应用程序中是默认的,该应用程序默认具有单个数据源。

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). 由于您已经创建了自己的PlatformTransactionManager bean,因此禁用了Spring Boot的默认事务管理器bean(名为transcationManager (根据TransactionAutoConfiguration Spring Boot自动配置逻辑)。

You most likely need to define one more transactionManager (and potentially dataSource ) for Camunda's process engine, which requires it's own schema. 您很可能需要为Camunda的流程引擎再定义一个transactionManager (可能还有dataSource ),这需要它自己的架构。 Make sure to use the right bean name as below: 确保使用正确的bean名称,如下所示:

@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. 从Spring 4开始,bean名称是自动连接时的默认限定符,因此新事务管理器将连接到DefaultDatasourceConfiguration因为它匹配类中的字段名称。

Alternatively don't use DefaultDatasourceConfiguration and roll out your own configuration if Spring Boot defaults are not working for you. 或者,如果Spring Boot默认设置不适合您,请不要使用DefaultDatasourceConfiguration并推出自己的配置。

Use @Qualifier annotation The @Qualifier annotation is used to resolve the autowiring conflict, when there are multiple beans of same type. 使用@Qualifier注释当有多个相同类型的bean时,@ Qualifier注释用于解决自动装配冲突。

@Bean
@Qualifier("ubldbTransactionManager")
protected PlatformTransactionManager transactionManager

and

@Bean
@Qualifier("bpdbTransactionManager")
public PlatformTransactionManager bpdbTransactionManager

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM