gpt4 book ai didi

java - Spring 批处理中的重写 Bean 问题

转载 作者:行者123 更新时间:2023-12-02 01:30:01 26 4
gpt4 key购买 nike

我有一个 Spring Batch 应用程序,我必须覆盖像 jobLauncher 这样的 bean。但是当我尝试运行命令 clean install 时,我遇到了 bean 覆盖问题。它在我的本地计算机上运行良好,但我在 Jenkins 上遇到了问题,我不知道为什么它没有在我的本地计算机上发生。

我查看了 spring 代码,他们扩展了配置类并覆盖了 bean。我尝试扩展 SimpleBatchConfiguration 类,但遇到了一些问题,我认为这不是一个好主意。

JobLaunher Bean in Application:

@Bean
public JobLauncher jobLauncher(JobRepository jobRepository) throws Exception {
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
jobLauncher.setJobRepository(jobRepository);
jobLauncher.afterPropertiesSet();
return jobLauncher;
}

JobLauncher in Spring Batch:

@Override
@Bean
public JobLauncher jobLauncher() throws Exception {
return createLazyProxy(jobLauncher, JobLauncher.class);
}

Error Logs:


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

Description:

The bean 'jobLauncher', defined in com.orange.alc.dabek.dataload.config.BatchConfiguration, could not be registered. A bean with that name has already been defined in class path resource [org/springframework/batch/core/configuration/annotation/SimpleBatchConfiguration.class] and overriding is disabled.

Action:

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

2019-05-20 20:26:45.056 ERROR 12892 --- [ main] o.s.test.context.TestContextManager : Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@29f8134] to prepare test instance [com.orange.alc.dabek.dataload.job.PhoneJobTest@611a2d82]

java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)
at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190)
at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246)
at

我想覆盖 Spring Batch Bean。我发现使用 spring.main.allow-bean-definition-overriding=true 不是一个好方法,因为它还隐藏了一些好处。我在 application.yml 中也没有找到相同的属性。请让我知道更好的解决方案。

最佳答案

为了自定义 Spring Batch 基础设施 bean(作业存储库、作业启动器、事务管理器等),您需要提供自定义 BatchConfigurer (请参阅引用文档的 JavaConfig 部分) .

您可以使批量配置类从 Spring Boot 或 org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer 扩展org.springframework.boot.autoconfigure.batch.BasicBatchConfigurer/code> 来自 Spring Batch 并重写 createJobLauncher 方法。

关于java - Spring 批处理中的重写 Bean 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56233013/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com