必须使用 EclipseLink
作为我的 Spring Batch 作业的 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 中,您可以看到我的依赖项以及我如何与 EclipseLink 交换 Hibernate:
<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>
除此之外,我还添加了在 Spring 中使用 EclipseLink 所需的 EclipseLinkJpaConfiguration
:
@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 。该修复计划在 5.0 版中进行,计划于 2022 年第四季度进行。
同时,您可以允许覆盖 Bean 定义或将您的 Bean 重命名为 transactionManager
以外的名称。
关于java - TransactionManager 已使用 EclipseLink JPA 在 Spring Batch 中定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69224745/