TransactionManager 已在 Spring 批处理中使用 EclipseLink JPA 定义

TransactionManager already defined in Spring Batch using EclipseLink JPA

必须使用 EclipseLink 作为我的 Spring 批处理作业的 JPA 实现,因为 Hibernate 缺少我需要的重要功能。

不幸的是,在将 Hibernate 与 EclipseLink 交换后,启动批处理 jar 时出现以下错误:

$ java -jar my-batch.jar

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-09-17 15:52:38.387 ERROR 15708 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   :

***************************
APPLICATION FAILED TO START
***************************

Description:

The bean 'transactionManager', defined in class path resource [org/springframework/batch/core/configuration/annotation/SimpleBatchConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [path/to/my/EclipseLinkJpaConfiguration.class] and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

在我当前的 pom.xml 中,您可以看到我的依赖项以及我如何将 Hibernate 与 EclipseLink 交换:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.3.12.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.3.12.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-entitymanager</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-core</artifactId>
            <version>4.2.7.RELEASE>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.jpa</artifactId>
            <version>2.7.5</version>
        </dependency>
    </dependencies>

除此之外,我还添加了 EclipseLinkJpaConfiguration,这是在 Spring 中使用 EclipseLink 所必需的:

@Configuration
public class EclipseLinkJpaConfiguration extends JpaBaseConfiguration {
    protected EclipseLinkJpaConfiguration(DataSource dataSource,
                                          JpaProperties properties,
                                          ObjectProvider<JtaTransactionManager> jtaTransactionManager) {
        super(dataSource, properties, jtaTransactionManager);
    }

    @Override
    protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
        return new EclipseLinkJpaVendorAdapter();
    }

    @Override
    protected Map<String, Object> getVendorProperties() {
        Map<String, Object> map = new HashMap<>();
        map.put(PersistenceUnitProperties.WEAVING, "false");
        map.put(PersistenceUnitProperties.LOGGING_LEVEL, SessionLog.FINER_LABEL);

        return map;
    }
}

我不知道是什么原因造成的。 添加 spring.main.allow-bean-definition-overriding=true 显然是一个临时解决方案,但我想解决这个问题。 我将不胜感激任何帮助!提前致谢

这是一个已知问题,请参阅 https://github.com/spring-projects/spring-batch/issues/816。计划在 2022 年第 4 季度发布 5.0 版进行修复。

同时,您可以允许 bean 定义覆盖或将 bean 重命名为 transactionManager 以外的名称。