【问题标题】:My job is always null. Can't inject a batch job with Spring Batch. Why?我的工作总是空的。无法使用 Spring Batch 注入批处理作业。为什么?
【发布时间】:2014-09-05 07:57:34
【问题描述】:

我正在使用 Spring Batch 来实现批处理作业,但由于某些原因,当我尝试开始我的工作时,我的工作总是为空。在我看来,我的工作没有在 Spring 框架需要时注入。 有人可以告诉我我做错了什么吗?!我有点厌倦了尝试和错误......

这是我的 spring 配置(批量配置):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch" xmlns:task="http://www.springframework.org/schema/task"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.2.xsd">

<bean id="batchTransactionManager"
    class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
<bean id="recordItemReader" class="com.blang.court.exports.RecordItemReader" />

<batch:job id="recordReadJob" job-repository="jobRepository">
    <batch:step id="recordReadStep">
        <batch:tasklet ref="recordItemReader"
            transaction-manager="batchTransactionManager" />
    </batch:step>
</batch:job>

<bean id="batchLauncher" class="com.blang.court.exports.BatchLauncher">
    <property name="jobLauncher" ref="jobLauncher" />
    <property name="job" ref="recordReadJob" />
</bean>

还有另一个 Spring 配置(应用程序上下文):

 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ws="http://jax-ws.dev.java.net/spring/core"
xmlns:wss="http://jax-ws.dev.java.net/spring/servlet" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"

xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://jax-ws.dev.java.net/spring/core
http://jax-ws.dev.java.net/spring/core.xsd
http://jax-ws.dev.java.net/spring/servlet
http://jax-ws.dev.java.net/spring/servlet.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd"
default-autowire="byName">

<context:annotation-config />

<tx:annotation-driven transaction-manager="applicationTransactionManager" />


<!-- Mail -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="localhost" />
</bean>


<!-- JPA -->
<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="appPersistenceUnit" />
</bean>

<bean id="applicationTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>


<!-- Spring Batch -->
<!-- <bean id="batchTransactionManager" 
    class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />-->

<bean id="jobRepository"
    class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
    <property name="transactionManager" ref="applicationTransactionManager" />
</bean>

<bean id="jobLauncher"
    class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    <property name="jobRepository" ref="jobRepository" />
</bean>

关于配置。在下一步中,我实现了批处理启动器类。看起来像这样:

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;

public class BatchLauncher
{
  private Job job;
  private JobLauncher jobLauncher;

  public void launch()
  {
  JobParametersBuilder builder = new JobParametersBuilder();

  try
  {
    JobExecution execution = jobLauncher.run(job, builder.toJobParameters());
  }
  catch (JobExecutionAlreadyRunningException | JobRestartException
          | JobInstanceAlreadyCompleteException
          | JobParametersInvalidException e)
  {
    e.printStackTrace();
  }

 }

 public Job getJob()
 {
   return job;
 }

 public void setJob(Job job)
 {
   this.job = job;
 }

 public JobLauncher getJobLauncher()
 {
    return jobLauncher;
 }

 public void setJobLauncher(JobLauncher jobLauncher)
 {
   this.jobLauncher = jobLauncher;
 }
}

最后是我的工作:

import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;

import com.blang.court.RecordForm;
import com.blang.court.interfaces.IRecordService;

public class RecordItemReader implements Tasklet
{
   @Autowired
   IRecordService recordService;

   @Override
   public RepeatStatus execute(StepContribution arg0, ChunkContext arg1)
      throws Exception
   {
      recordService.filterRecords(new RecordForm());
      System.out.println("Hi, the job is running! Yeah, finally got it!");
      return RepeatStatus.FINISHED;
   }
 }

我总是在批处理启动器类的这一行上得到一个nullPointerException

JobExecution execution = jobLauncher.run(job, builder.toJobParameters());

这告诉我我的 jo 是空的。我已经对其进行了调试以证明它......是的,由于某种原因,这项工作是空的......

请帮帮我... :-(

亲切的问候

【问题讨论】:

  • 你在哪里声明这个bean“billrecordItemReader”?
  • @Xstian 在批处理配置中。线程中的第一个配置 xml。 <bean id="recordItemReader" class="com.blang.court.exports.RecordItemReader"></bean>
  • id 是 "recordItemReader" 你应该使用 而不是
  • @Xstian 谢谢你的提示。我纠正了它,但不幸的是它并没有解决我的问题。这项工作仍然是空的......:/
  • Job id 是 "recordReadJob" :) 试试 Tasklet 不是 Job :)

标签: spring batch-processing spring-batch


【解决方案1】:

在您的配置中,job 属性指的是一个 Tasklet 而不是一个 Job..

试试这个配置

<bean id="batchLauncher" class="com.blang.court.exports.BatchLauncher">
    <property name="jobLauncher" ref="jobLauncher" />
    <property name="job" ref="recordReadJob" />
</bean>

希望对你有帮助


当你通过 new 创建对象时,autowire\inject 不起作用...

示例类

public class Test {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("app-context.xml");
        BatchLauncher batchLauncher = (BatchLauncher ) context.getBean("batchLauncher");
        batchLauncher.launch();
    }
} 

这个link解决了同样的问题

【讨论】:

  • 再次感谢您!我更正了它,但这也没有修复 NPE... :-( 我认识到 batchlauncher 类中的 jobRepository 也是空的....
  • 当我使用 spring-batch 时 .. 我将此 bean 用于 jobRepository ..
  • 我从几个教程中获得了这个存储库,我在实施之前已经阅读过。他们都在使用我在代码中使用的存储库......所以,我认为这不会解决我的问题。 ;-) 无论如何,谢谢你的提示。 ;-)
  • 更正:我想写的是 jobLauncher 是 null 而不是 jobRepository。我没有在 Batchlauncher 类中使用 jobRepository。只是为了协议! :-)
  • 问题是这个.. :) .. 你应该从 spring 上下文中检索这个 bean,否则注入不起作用
猜你喜欢
  • 2020-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-06
  • 1970-01-01
  • 2016-11-04
  • 2019-07-26
  • 2021-09-04
相关资源
最近更新 更多