Spring-batch @BeforeStep does not work with @StepScope

前端 未结 1 799
长发绾君心
长发绾君心 2020-11-29 03:19

I\'m using Spring Batch version 2.2.4.RELEASE I tried to write a simple example with stateful ItemReader, ItemProcessor and ItemWriter beans.

public class St         


        
相关标签:
1条回答
  • 2020-11-29 03:23

    When you configure a bean as follows:

    @Bean
    @StepScope
    public MyInterface myBean() {
        return new MyInterfaceImpl();
    }
    

    You are telling Spring to use the proxy mode ScopedProxyMode.TARGET_CLASS. However, by returning the MyInterface, instead of the MyInterfaceImpl, the proxy only has visibility into the methods on the MyInterface. This prevents Spring Batch from being able to find the methods on MyInterfaceImpl that have been annotated with the listener annotations like @BeforeStep. The correct way to configure this is to return MyInterfaceImpl on your configuration method like below:

    @Bean
    @StepScope
    public MyInterfaceImpl myBean() {
        return new MyInterfaceImpl();
    }
    

    We have added a warning log message on startup that points out, as we look for the annotated listener methods, if the object is proxied and the target is an interface, we won't be able to find methods on the implementing class with annotations on them.

    0 讨论(0)
提交回复
热议问题