SpringBatch应用程序使用JDBC进行读取,但无法启动

huangapple go评论117阅读模式
英文:

SpringBatch App Using JDBC for Reading is Failing to Start

问题

这是一个非常相似的代码示例(由于我需要进行一些混淆)。我遇到了一个应用程序启动失败的错误。没有显示数据源 bean 和 Spring Boot 应用程序类的代码。当我设置断点并在调试模式下运行时,除了似乎完全跳过的 Job 和 Step bean 外,所有的 bean 似乎都被创建了。我不确定如何进一步诊断。似乎是一些 Spring 魔术问题。非常感谢任何想法。

以下是异常信息:

2020-08-23 11:26:50.264 INFO 12195 --- [ main] o.apache.catalina.core.StandardService : 正在启动服务 [Tomcat]
2020-08-23 11:26:50.265 INFO 12195 --- [ main] org.apache.catalina.core.StandardEngine : 正在启动 Servlet 引擎:[Apache Tomcat/9.0.31]
2020-08-23 11:26:50.382 INFO 12195 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : 初始化 Spring 嵌入式 WebApplicationContext
2020-08-23 11:26:50.383 INFO 12195 --- [ main] o.s.web.context.ContextLoader : 根 WebApplicationContext:初始化完成,耗时 2276 毫秒
2020-08-23 11:26:57.552 WARN 12195 --- [ main] ConfigServletWebServerApplicationContext : 在初始化上下文期间遇到异常 - 正在取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException: 错误创建 bean,名称为 'databaseCursorStep',定义在类路径资源 [/com/configuration/BatchConfig.class] 中:通过方法 'databaseCursorStep' 的参数 0 表达的不满足的依赖项;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 不存在类型为 'org.springframework.batch.item.ItemReader<com.dto.StuffDto>' 的限定的 bean:预期至少有 1 个限定为自动装配候选的 bean。依赖注解:{@org.springframework.beans.factory.annotation.Qualifier(value=databaseCursorItemReader)}
2020-08-23 11:26:57.572 INFO 12195 --- [ main] o.apache.catalina.core.StandardService : 正在停止服务 [Tomcat]
2020-08-23 11:26:57.603 INFO 12195 --- [ main] ConditionEvaluationReportLoggingListener :

启动 ApplicationContext 发生错误。要显示条件报告,请使用 'debug' 模式重新运行应用程序。
2020-08-23 11:26:57.908 ERROR 12195 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :


应用程序启动失败


描述:

com.configuration.BatchConfig 中的方法 databaseCursorStep 的参数 0 需要一种类型为 'org.springframework.batch.item.ItemReader' 的 bean,但找不到。

注入点具有以下注解:
- @org.springframework.beans.factory.annotation.Qualifier(value=databaseCursorItemReader)

操作:

考虑在配置中定义一种类型为 'org.springframework.batch.item.ItemReader' 的 bean。

与目标虚拟机断开连接,地址:'127.0.0.1:46088',传输:'socket'。

使用以下代码:

@Configuration
@EnableBatchProcessing
public class BatchConfig {

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    private static final String GET_DATA =
            "SELECT " +
            "stuffA, " +
            "stuffB, " +
            "FROM STUFF_TABLE " +
            "ORDER BY stuffA ASC";

    @Bean
    public ItemReader<StuffDto> itemReader(DataSource dataSource) {
        return new JdbcCursorItemReaderBuilder<StuffDto>()
            .name("cursorItemReader")
            .dataSource(dataSource)
            .sql(GET_DATA)
            .rowMapper(new BeanPropertyRowMapper<>(StuffDto.class))
            .build();
    }

    @Bean
    ItemProcessor<StuffDto, StuffDto> databaseXmlItemProcessor() {
        return new QueryLoggingProcessor();
    }

    @Bean
    public ItemWriter<StuffDto> databaseCursorItemWriter() {
        return new LoggingItemWriter();
    }

    @Bean
    public Step databaseCursorStep(@Qualifier("databaseCursorItemReader") ItemReader<StuffDto> reader,
                                   @Qualifier("databaseCursorItemWriter") ItemWriter<StuffDto> writer,
                                   StepBuilderFactory stepBuilderFactory) {
        return stepBuilderFactory.get("databaseCursorStep")
            .<StuffDto, StuffDto>chunk(1)
            .reader(reader)
            .writer(writer)
            .build();
    }

    @Bean
    public Job databaseCursorJob(@Qualifier("databaseCursorStep") Step exampleJobStep,
                                 JobBuilderFactory jobBuilderFactory) {
        return jobBuilderFactory.get("databaseCursorJob")
            .incrementer(new RunIdIncrementer())
            .flow(exampleJobStep)
            .end()
            .build();
    }
}

希望对您有所帮助!

英文:

I have something very similar to the code below (I had to do some obfiscation). I am getting an Application Failed to Start error. Code not shown are datasource bean and spring boot application class. When I put breakpoints in and all run in debug, all beans appear to be created except the Job and Step bean, which seem to be skipped over entirely. I am not sure how to diagnose further. Seems to be some Spring Magic issues. Any ideas are greatly appreciated.

Here is the exception:

2020-08-23 11:26:50.264 INFO 12195 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-08-23 11:26:50.265 INFO 12195 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.31]
2020-08-23 11:26:50.382 INFO 12195 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-08-23 11:26:50.383 INFO 12195 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2276 ms
2020-08-23 11:26:57.552 WARN 12195 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'databaseCursorStep' defined in class path resource [/com/configuration/BatchConfig.class]: Unsatisfied dependency expressed through method 'databaseCursorStep' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.batch.item.ItemReader<com.dto.StuffDto>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=databaseCursorItemReader)}
2020-08-23 11:26:57.572 INFO 12195 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2020-08-23 11:26:57.603 INFO 12195 --- [ main] ConditionEvaluationReportLoggingListener :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-08-23 11:26:57.908 ERROR 12195 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :


APPLICATION FAILED TO START


Description:

Parameter 0 of method databaseCursorStep in com.configuration.BatchConfig required a bean of type 'org.springframework.batch.item.ItemReader' that could not be found.

The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Qualifier(value=databaseCursorItemReader)

Action:

Consider defining a bean of type 'org.springframework.batch.item.ItemReader' in your configuration.

Disconnected from the target VM, address: '127.0.0.1:46088', transport: 'socket'

Process finished with exit code 1

Here is the code:

@Configuration
@EnableBatchProcessing
public class BatchConfig {
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
private static final String GET_DATA =
&quot;SELECT &quot; +
&quot;stuffA, &quot; +
&quot;stuffB, &quot; +
&quot;FROM STUFF_TABLE &quot; +
&quot;ORDER BY stuffA ASC&quot;;
@Bean
public ItemReader&lt;StuffDto&gt; itemReader(DataSource dataSource) {
return new JdbcCursorItemReaderBuilder&lt;StuffDto&gt;()
.name(&quot;cursorItemReader&quot;)
.dataSource(dataSource)
.sql(GET_DATA)
.rowMapper(new BeanPropertyRowMapper&lt;&gt;(StuffDto.class))
.build();
}
@Bean
ItemProcessor&lt;StuffDto, StuffDto&gt; databaseXmlItemProcessor() {
return new QueryLoggingProcessor();
}
@Bean
public ItemWriter&lt;StuffDto&gt; databaseCursorItemWriter() {
return new LoggingItemWriter();
}
@Bean
public Step databaseCursorStep(@Qualifier(&quot;databaseCursorItemReader&quot;) ItemReader&lt;StuffDto&gt; reader,
@Qualifier(&quot;databaseCursorItemWriter&quot;) ItemWriter&lt;StuffDto&gt; writer,
StepBuilderFactory stepBuilderFactory) {
return stepBuilderFactory.get(&quot;databaseCursorStep&quot;)
.&lt;StuffDto, StuffDto&gt;chunk(1)
.reader(reader)
.writer(writer)
.build();
}
@Bean
public Job databaseCursorJob(@Qualifier(&quot;databaseCursorStep&quot;) Step exampleJobStep,
JobBuilderFactory jobBuilderFactory) {
return jobBuilderFactory.get(&quot;databaseCursorJob&quot;)
.incrementer(new RunIdIncrementer())
.flow(exampleJobStep)
.end()
.build();
}
}

答案1

得分: 0

您的限定符(Qualifier)Bean名称应为 itemReader,而不是 databaseCursorItemReader。请更改方法名称或更改为 databaseCursorStep(@Qualifier("itemReader")

@Bean
public Step databaseCursorStep(@Qualifier("itemReader") ItemReader<StuffDto> reader,
                               @Qualifier("databaseCursorItemWriter") ItemWriter<StuffDto> writer,
                               StepBuilderFactory stepBuilderFactory) {
    return stepBuilderFactory.get("databaseCursorStep")
        .<StuffDto, StuffDto>chunk(1)
        .reader(reader)
        .writer(writer)
        .build();
}

@Bean
public ItemReader<StuffDto> itemReader(DataSource dataSource) {
    return new JdbcCursorItemReaderBuilder<StuffDto>()
        .name("cursorItemReader")
        .dataSource(dataSource)
        .sql(GET_DATA)
        .rowMapper(new BeanPropertyRowMapper<>(StuffDto.class))
        .build();
}
英文:

Your Qualifier Bean name is itemReader not databaseCursorItemReader. Either change the method name or change to databaseCursorStep(@Qualifier(&quot;itemReader&quot;)

@Bean
public Step databaseCursorStep(@Qualifier(&quot;itemReader&quot;) ItemReader&lt;StuffDto&gt; reader,
@Qualifier(&quot;databaseCursorItemWriter&quot;) ItemWriter&lt;StuffDto&gt; writer,
StepBuilderFactory stepBuilderFactory) {
return stepBuilderFactory.get(&quot;databaseCursorStep&quot;)
.&lt;StuffDto, StuffDto&gt;chunk(1)
.reader(reader)
.writer(writer)
.build();
}
@Bean
public ItemReader&lt;StuffDto&gt; itemReader(DataSource dataSource) {
return new JdbcCursorItemReaderBuilder&lt;StuffDto&gt;()
.name(&quot;cursorItemReader&quot;)
.dataSource(dataSource)
.sql(GET_DATA)
.rowMapper(new BeanPropertyRowMapper&lt;&gt;(StuffDto.class))
.build();
}

huangapple
  • 本文由 发表于 2020年8月23日 23:53:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63549016.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定