这是我的代码,我不知道为什么我的bean MouvementToMapItemProcessor
没有注入,它在构造函数中始终为null
@Autowired
private MouvementToMapItemProcessor mvts;
private Iterator it;
public InMemoryMouvementReader() {
it = mvts.getMouvmentFileRowMap().entrySet().iterator();
}
这是我的配置类:
@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;
}
我的堆栈跟踪
启动ApplicationContext时出错。要显示自动配置报告,请使用&#39; debug&#39;重新运行您的应用程序。启用。 11:55:29.139 [restartedMain] ERROR o.s.boot.SpringApplication - 应用程序启动失败 org.springframework.beans.factory.BeanCreationException:创建名为&#39; mouvementMapReader&#39;的bean时出错。在类路径资源[fr / gouv / justice / spark / fileToBaseBatch / BatchConfiguration.class]中定义:通过工厂方法的Bean实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.batch.item.ItemReader]:工厂方法&mouvementMapReader&#39;抛出异常;嵌套异常是java.lang.NullPointerException 在org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306) 在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 在org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) 在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) 在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) 在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) 在org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) 在org.springframework.boot.SpringApplication.run(SpringApplication.java:303) 在org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) 在org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) 在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) 在org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) 引起:org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.batch.item.ItemReader]:工厂方法&mouvementMapReader&#39;抛出异常;嵌套异常是java.lang.NullPointerException 在org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) 在org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ...省略了22个常用帧 引起: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() 在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) 在org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ...省略了23个常见帧
答案 0 :(得分:0)
你的MouvementToMapItemProcessor
在构造之前没有被注入InMemoryMouvementReader
,这是导致构造函数失败并出现NullPointerException的原因。请改用构造函数注入:
@Autowired
public InMemoryMouvementReader(MouvementToMapItemProcessor mvts) {
this.mvts = mvts;
it = mvts.getMouvmentFileRowMap().entrySet().iterator();
}
和
@Bean
public ItemReader<MouvementFileRow> mouvementMapReader(MouvementToMapItemProcessor mvts) {
return new InMemoryMouvementReader(mvts);
}
和
@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();
}