我使用的是Spring引导版1.4.2.RELEASE和sprig批处理框架。 我试图从文件导入数据,然后将结果存储在数据库中,我处理动态提交间隔号,所以我实现了一个方法,返回输入文件的行数并传递它作为参数使用" jobParameters":
我的批处理作业配置如下所示:
<beans:bean id="stepScope" class="org.springframework.batch.core.scope.StepScope">
<beans:property name="autoProxy" value="true"/>
</beans:bean>
<beans:bean id="MyProcessor" class="org.job.step.MyProcessor" scope="step" />
<beans:bean id="MyWriter" class="org.job.step.MyWriter" scope="step" />
<beans:bean id="jobCompleteNotificationListener" class="org.job.JobCompletionNotificationListener" />
<job id="MyImportJob">
<step id="step1">
<tasklet transaction-manager="transactionManager">
<chunk reader="MyReader"
processor="MyProcessor"
writer="MyWriter"
commit-interval="#{jobParameters['commit.interval']}" >
</chunk>
</tasklet>
</step>
<listeners>
<listener ref="jobCompleteNotificationListener" />
</listeners>
</job>
<beans:bean id="headerBasedDelimitedLineTokenizer" scope="step"
class="org.job.custom.HeaderBasedDelimitedLineTokenizer">
<beans:property name="delimiter" value=";" />
</beans:bean>
<!-- <beans:bean id="customLineTokenizer" class="org.job.step.CustomLineTokenizer"/> -->
<beans:bean id="MyReader" scope="step"
class="org.springframework.batch.item.file.FlatFileItemReader">
<beans:property name="resource" value="file:/#{jobParameters['input.file.name']}" />
<beans:property name="encoding" value="UTF-8" />
<beans:property name="strict" value="false" />
<beans:property name="linesToSkip" value="1" />
<beans:property name="skippedLinesCallback">
<beans:ref bean="headerBasedDelimitedLineTokenizer" />
</beans:property>
<beans:property name="lineMapper">
<beans:bean
class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<beans:property name="lineTokenizer">
<beans:ref bean="headerBasedDelimitedLineTokenizer" />
</beans:property>
<beans:property name="fieldSetMapper">
<beans:bean class="org.job.step.MyFieldSetMapper" />
</beans:property>
</beans:bean>
</beans:property>
</beans:bean>
当首次运行spring boot应用程序时,它会抛出一个异常:
2017-04-07 15:03:04.537 -ERROR [PID=8692] org.springframework.batch.core.step.AbstractStep.execute : Encountered an error executing step step1 in job MyImportJob
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'scopedTarget.org.springframework.batch.repeat.policy.SimpleCompletionPolicy#0': Unsatisfied dependency expressed through constructor parameter 0: Could not convert argument value of type [null] to required type [int]: Failed to convert value of type 'null' to required type 'int'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type 'null' to required type 'int': PropertyEditor [org.springframework.beans.propertyeditors.CustomNumberEditor] returned inappropriate value of type 'null'
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:723)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1148)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1050)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:512)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$2.getObject(AbstractBeanFactory.java:345)
at org.springframework.batch.core.scope.StepScope.get(StepScope.java:113)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:340)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:192)
at com.sun.proxy.$Proxy50.start(Unknown Source)
at org.springframework.batch.repeat.support.RepeatTemplate.start(RepeatTemplate.java:468)
at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:169)
at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)
at org.springframework.batch.core.step.item.SimpleChunkProvider.provide(SimpleChunkProvider.java:110)
at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:69)
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:406)
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:330)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:271)
at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:81)
at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:374)
at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)
at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:257)
at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:200)
at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148)
at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64)
at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67)
at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169)
at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144)
at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:134)
at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:306)
at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135)
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128)
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.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:127)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy75.run(Unknown Source)
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.execute(JobLauncherCommandLineRunner.java:216)
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.executeLocalJobs(JobLauncherCommandLineRunner.java:233)
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.launchJobFromProperties(JobLauncherCommandLineRunner.java:125)
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.run(JobLauncherCommandLineRunner.java:119)
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:800)
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:784)
at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:771)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175)
答案 0 :(得分:1)
Spring尝试注入一个int类型的构造函数-arg,但该值为null。
null无法转换为int,这就是您面对异常的原因。
从你的描述我猜,春天的表达
#{jobParameters['commit.interval']}
结果为空。
使用调试器查看实际注入bean的内容&#39; transactionManager&#39;