0
votes

Here is my code, I don't know why my bean MouvementToMapItemProcessor is not injected, it's always null in constructor

@Autowired
    private MouvementToMapItemProcessor mvts;

    private Iterator it;

    public InMemoryMouvementReader() {
        it = mvts.getMouvmentFileRowMap().entrySet().iterator();
    }

Here is my configuration class:

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {


    public BatchConfiguration() {
    }


    @Bean
    public ItemReader<MouvementFileRow> mouvementMapReader() {
        return new InMemoryMouvementReader();
    }


    @Bean
    public ItemProcessor<MouvementFileRow, MouvementFileRow> mouvementMapProcessor() {
        return new MouvementToMapItemProcessor();
    }



    @Bean
    public Step generateDemmandeCommunication() {
        return stepBuilderFactory.get("generateDemmandeCommunication")
                .<MouvementFileRow, DemandeCommunication>chunk(10)
                .faultTolerant().skipLimit(Integer.MAX_VALUE).skip(CustomReaderSkipException.class)
                .reader(mouvementMapReader())
                .processor(mouvementProcessor())
                .writer(demandeCommunicationItemWriter())
                .listener(customStepListener())
                .build();
    }


public class InMemoryMouvementReader implements ItemReader<MouvementFileRow> {

    @Autowired
    private MouvementToMapItemProcessor mvts;

    private Iterator it;

    public InMemoryMouvementReader() {
        it = mvts.getMouvmentFileRowMap().entrySet().iterator();
    }

    private void initialize() {
    }

    @Override
    public MouvementFileRow read() throws Exception {
        if (it.hasNext()) {
            return mvts.getMouvmentFileRowMap().get(it.next());
        } else return null;
    }
}


@Component
public class MouvementToMapItemProcessor implements ItemProcessor<MouvementFileRow, MouvementFileRow> {

    private static final Logger log = LoggerFactory.getLogger(MouvementToMapItemProcessor.class);

    private Map<Long, MouvementFileRow> mouvmentFileRowMap;

    public MouvementToMapItemProcessor() {
        mouvmentFileRowMap = new HashMap<Long, MouvementFileRow>();

    }

    @Override
    public MouvementFileRow process(final MouvementFileRow mouvement) throws Exception {
        ........
        return null;
    }

    public Map<Long, MouvementFileRow> getMouvmentFileRowMap() {
        return mouvmentFileRowMap;
    }

My stack trace

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled. 11:55:29.139 [restartedMain] ERROR o.s.boot.SpringApplication - Application startup failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mouvementMapReader' defined in class path resource [fr/gouv/justice/spark/fileToBaseBatch/BatchConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.item.ItemReader]: Factory method 'mouvementMapReader' threw exception; nested exception is java.lang.NullPointerException at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) at fr.gouv.justice.spark.fileToBaseBatch.Application.main(Application.java:18) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.item.ItemReader]: Factory method 'mouvementMapReader' threw exception; nested exception is java.lang.NullPointerException at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ... 22 common frames omitted Caused by: java.lang.NullPointerException: null at fr.gouv.justice.spark.fileToBaseBatch.readers.InMemoryMouvementReader.(InMemoryMouvementReader.java:19) at fr.gouv.justice.spark.fileToBaseBatch.BatchConfiguration.mouvementMapReader(BatchConfiguration.java:74) at fr.gouv.justice.spark.fileToBaseBatch.BatchConfiguration$$EnhancerBySpringCGLIB$$e3e5804e.CGLIB$mouvementMapReader$34() at fr.gouv.justice.spark.fileToBaseBatch.BatchConfiguration$$EnhancerBySpringCGLIB$$e3e5804e$$FastClassBySpringCGLIB$$8f516405.invoke() at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358) at fr.gouv.justice.spark.fileToBaseBatch.BatchConfiguration$$EnhancerBySpringCGLIB$$e3e5804e.mouvementMapReader() at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ... 23 common frames omitted

1

1 Answers

0
votes

Your MouvementToMapItemProcessor is not injected into InMemoryMouvementReader until after it is constructed, which is what causes the constructor to fail with a NullPointerException. Use constructor injection instead:

@Autowired
public InMemoryMouvementReader(MouvementToMapItemProcessor mvts) {
    this.mvts = mvts;
    it = mvts.getMouvmentFileRowMap().entrySet().iterator();
}

and

@Bean
public ItemReader<MouvementFileRow> mouvementMapReader(MouvementToMapItemProcessor mvts) {
    return new InMemoryMouvementReader(mvts);
}

and

@Bean
public Step generateDemmandeCommunication(InMemoryMouvementReader reader, MouvementToMapItemProcessor mvts) {
    return stepBuilderFactory.get("generateDemmandeCommunication")
            .<MouvementFileRow, DemandeCommunication>chunk(10)
            .faultTolerant().skipLimit(Integer.MAX_VALUE).skip(CustomReaderSkipException.class)
            .reader(reader)
            .processor(mvts)
            .writer(demandeCommunicationItemWriter())
            .listener(customStepListener())
            .build();
}