【问题标题】:Spring Batch StepScope and Proxies. AgainSpring Batch StepScope 和代理。再次
【发布时间】:2022-10-13 18:03:38
【问题描述】:

我正在尝试保存与步骤相关的状态,该状态可以从处理器访问。为此,我为它创建了一个类和一个 bean。我的配置文件如下所示:

@Slf4j
@Configuration
@EnableScheduling
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MyConfiguration
{
    // Job, reader and writer beans

    @Bean("myStep")
    Step myStep(@Qualifier("myReader") ItemReader<InputEntity> reader,
            @Qualifier("myProcessor") ItemProcessor<InputEntity, OutputEntity> processor,
            @Qualifier("myWriter") ItemWriter<OutputEntity> writer)
    {
        return stepBuilderFactory
                .get("myStep")
                .<InputEntity, OutputEntity> chunk(100)
                .reader(reader)
                .processor(processor)
                .writer(writer)
                .build();
    }

    @StepScope
    @Bean("myProcessor")
    public MyProcessor processingStep(StateService s)
    {
        var processor = new MyProcessor();
        processor.setStateService(s);
        return processor;
    }

    @Scope(value = "step", proxyMode = ScopedProxyMode.NO)
    @Bean
    public StateService stateService()
    {
        return new StateService();
    }

}

背后的想法是为每个新的步骤执行创建一个状态服务(该类目前为空,并且没有@Component 注释)。但是,我遇到了 Spring 代理的麻烦:

org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'MyProcessor' is expected to be of type 'very.long.package.name.steps.MyProcessor' but was actually of type 'com.sun.proxy.$Proxy265'

收集已经回答的问题和我尝试过的几十个指南:

  • stateService bean 的所有可能代理模式;
  • 通过@Autowired变量将这个bean直接注入MyProcessor
  • 使用@EnableBatchProcessing 注释配置
  • 直接调用stateService() bean:processor.setStateService(stateService());
  • 将 bean 注入步骤 Step bean。在这种情况下,我必须更改方法签名,因此该方法接受 MyProcessor 而不是 ItemProcessor&lt;InputEntity, OutputEntity&gt; 来公开变量

没有任何帮助,我仍然得到这个例外。我对@StepScope 的概念有什么误解?如何存储某些状态以执行特定步骤?

我读过thisthis 甚至this,但都没有帮助我理解它。

【问题讨论】:

    标签: java spring-batch


    【解决方案1】:

    好吧,在这种情况下,添加一个接口会有所帮助:

    @Scope(value = "step", proxyMode = ScopedProxyMode.INTERFACES)
    @Bean
    public IStateService stateService()
    {
        return new StateService();
    }
    

    但是,它仍然没有回答为什么这个问题,也没有带来任何理解。

    【讨论】:

      猜你喜欢
      • 2016-04-17
      • 1970-01-01
      • 2020-09-25
      • 2020-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多