春季启动:如何限制Bean的可见性

时间:2018-11-19 10:23:11

标签: java spring spring-boot visibility camunda

我将两个自定义的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的可见性,以使第三方库无法访问它们?

2 个答案:

答案 0 :(得分:3)

提供

Private Sub CommandButton1_Click() Dim month As String Dim year As String Dim c As Range Dim d As Range Dim x As Long Dim k As Integer Dim source As Worksheet Dim targetforecastmonth As Worksheet 'change worksheet designations as needed Set source = ActiveWorkbook.Worksheets("Overall Sheet") Set targetforecastmonth = ActiveWorkbook.Worksheets("Forecast Month") targetforecastmonth.Range("A4:Z1000").Clear month = ActiveWorkbook.Worksheets("Forecast Month").Range("B1") year = ActiveWorkbook.Worksheets("Forecast Month").Range("D1") k = 4 x = 4 For Each c In source.Range("O4:O1000") Set d = source.Range("P" & x) If c.Value = month And d.Value = year Then source.Rows(c.Row).Copy targetforecastmonth.Rows(k) k = k + 1 End If x = x + 1 Next c End Sub 以使用默认的Spring Bean,例如名为DefaultDatasourceConfiguration的{​​{1}}和名为DataSource的{​​{1}}。可以将Camunda粘贴到Spring Boot应用程序中,该应用程序默认具有单个数据源。

由于您创建了自己的dataSource bean,因此禁用了Spring Boot的默认事务管理器bean,名为PlatformTransactionManager(根据transcationManager Spring Boot自动配置逻辑)。

您很可能需要为Camunda的流程引擎定义一个PlatformTransactionManager(甚至可能是transcationManager),这需要它自己的架构。确保使用正确的bean名称,如下所示:

TransactionAutoConfiguration

从Spring 4开始,bean名称是自动装配时的默认限定符,因此,新的事务管理器将与transactionManager相连,因为它与类中的字段名称匹配。

或者,如果Spring Boot的默认设置不适合您,则不要使用dataSource并推出自己的配置。

答案 1 :(得分:0)

使用@Qualifier批注 当有多个相同类型的bean时,@ Qualifier批注用于解决自动装配冲突。

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

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