2

我正在尝试使用 JSR-352 API 和 Spring Batch 3.0.4 作为实现来实现示例批处理应用程序。

在创建名为“batchPropertyPostProcessor”的 bean 时,批处理作业执行在初始化阶段失败:

Exception in thread "main" javax.batch.operations.JobStartException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'batchPropertyPostProcessor': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.batch.core.jsr.launch.support.BatchPropertyBeanPostProcessor.setBatchPropertyContext(org.springframework.batch.core.jsr.configuration.support.BatchPropertyContext); nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [dataListingStepListener] for bean with name 'scopedTarget.dataListingStepListener' defined in null; nested exception is java.lang.ClassNotFoundException: dataListingStepListener
    at org.springframework.batch.core.jsr.launch.JsrJobOperator.start(JsrJobOperator.java:637)
    at x98.BatchRunner.main(BatchRunner.java:18)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'batchPropertyPostProcessor': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.batch.core.jsr.launch.support.BatchPropertyBeanPostProcessor.setBatchPropertyContext(org.springframework.batch.core.jsr.configuration.support.BatchPropertyContext); nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [dataListingStepListener] for bean with name 'scopedTarget.dataListingStepListener' defined in null; nested exception is java.lang.ClassNotFoundException: dataListingStepListener
    at org.springframework.batch.core.jsr.configuration.support.SpringAutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(SpringAutowiredAnnotationBeanPostProcessor.java:262)
    at org.springframework.batch.core.jsr.configuration.support.JsrAutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(JsrAutowiredAnnotationBeanPostProcessor.java:30)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:232)
    at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:618)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:467)
    at org.springframework.batch.core.jsr.launch.JsrJobOperator.start(JsrJobOperator.java:635)
    ... 1 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.batch.core.jsr.launch.support.BatchPropertyBeanPostProcessor.setBatchPropertyContext(org.springframework.batch.core.jsr.configuration.support.BatchPropertyContext); nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [dataListingStepListener] for bean with name 'scopedTarget.dataListingStepListener' defined in null; nested exception is java.lang.ClassNotFoundException: dataListingStepListener
    at org.springframework.batch.core.jsr.configuration.support.SpringAutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(SpringAutowiredAnnotationBeanPostProcessor.java:575)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.batch.core.jsr.configuration.support.SpringAutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(SpringAutowiredAnnotationBeanPostProcessor.java:259)
    ... 13 more
Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [dataListingStepListener] for bean with name 'scopedTarget.dataListingStepListener' defined in null; nested exception is java.lang.ClassNotFoundException: dataListingStepListener
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1325)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:594)
    at org.springframework.beans.factory.support.AbstractBeanFactory.isTypeMatch(AbstractBeanFactory.java:526)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:387)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:354)
    at org.springframework.beans.factory.BeanFactoryUtils.beanNamesForTypeIncludingAncestors(BeanFactoryUtils.java:187)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1002)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
    at org.springframework.batch.core.jsr.configuration.support.SpringAutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(SpringAutowiredAnnotationBeanPostProcessor.java:532)
    ... 15 more
Caused by: java.lang.ClassNotFoundException: dataListingStepListener
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at org.springframework.util.ClassUtils.forName(ClassUtils.java:247)
    at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:395)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1346)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1317)
    ... 24 more

我的批处理运行器:

public class BatchRunner {

    public static void main(String[] args) {

        System.setProperty("JSR-352-BASE-CONTEXT", "x98_batch_local.xml");

        Properties jobParameters = new Properties();
        jobParameters.put("message", "Hello!");

        JobOperator jobOperator = BatchRuntime.getJobOperator();
        jobOperator.start("x98SampleJob", jobParameters);

    }
}

我的 sampleJob 定义:

<?xml version="1.0" encoding="UTF-8"?>
<job id="x98SampleJob" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/jobXML_1_0.xsd"
    version="1.0">

    <step id="simple" next="dataListing">
        <batchlet ref="simpleBatchlet">
            <properties>
                <property name="message" value="#{jobParameters['message']}" />
            </properties>
        </batchlet>
    </step>

    <step id="dataListing">
        <listeners>
            <listener ref="dataListingStepListener"/>
        </listeners>
        <chunk item-count="3">
            <reader ref="dataListingItemReader" />
            <processor ref="dataListingItemProcessor"/>
            <writer ref="dataListingItemWriter"/>
        </chunk>
    </step>
</job>  

我的阅读器、编写器、处理器和侦听器的 Spring bean 配置

<?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: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://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd">

    <import resource="classpath*:x98_services.xml"/>

    <bean name="simpleBatchlet" class="x98.batch.SimpleBatchlet"/>

    <bean name="dataListingItemProcessor" class="x98.batch.DataListingItemProcessor"/>

    <bean name="dataListingItemReader" class="x98.batch.DataListingItemReader">
        <property name="tx98DatasService" ref="tx98DatasService"/>
        <property name="tx98StructureService" ref="tx98StructureService"/>
    </bean>

    <bean name="dataListingItemWriter" class="x98.batch.DataListingItemWriter"/>

    <bean name="dataListingStepListener" class="x98.batch.DataListingStepListener">
        <property name="tx98StatusService" ref="tx98StatusService"/>
    </bean>
</beans>

读取器、写入器和处理器由 Spring 实例化并在作业运行中正确使用。当我将侦听器添加到作业定义时,它停止工作。

我调试了代码,我看到 dataListingStepListener bean 在 Spring 上下文中被初始化。我不明白为什么对侦听器(<listener ref="dataListingStepListener"/>)的引用不被识别为 Spring bean,而 Spring 正在尝试加载名为“dataListingStepListener”的类。

我的配置好吗?难道我做错了什么?

4

1 回答 1

1

回顾 Gist 后,有几点注意事项:

作业特定的 bean 必须在子上下文中
Spring Batch 的 JSR-352 上下文管理通过父/子关系工作。父上下文由 baseContext.xml 定义,或者可以像您通过系统属性所做的那样被覆盖JSR-352-BASE-CONTEXT。该上下文旨在仅用于基础设施 bean,并且不会由 JSR-352 后处理器处理以进行属性注入等操作。您说“当我将侦听器添加到作业定义时,它停止工作。” 如果您仔细观察,当作业运行时,您的属性没有被注入SimpleBatchlet(消息是null)。

这样做的原因是,如果您想运行多个作业,则不需要创建多个JobRepository实例、多个连接池等,因此我们只引导父上下文一次。由于我们只引导它一次,因此我们当时没有要注入的作业属性之类的值(并且它们可能会因作业而异)。

因此,所有特定于作业的 bean 都应该在子上下文中。

为 JSR-352 定义子上下文
JSR-352 提供了三种不同的方式来处理 bean 创建:

  1. 内联 -在作业 XML 的工件定义的属性中提供完全限定的类名,ref它将使用无参数构造函数创建。
  2. batch.xml - batch.xml 文件提供了一种通过为类名提供别名来简化上述概念的方法。
  3. 特定于实现的 DI(在这种情况下为 Spring DI)- Spring Batch 的 JSR-352 实现提供了使用 Spring 的 DI 工具来创建 bean 的能力。

有了这个,我们将控制权交给您如何创建 bean 实例。默认情况下,在 Spring 中,bean 是在启动时创建的单例。但是,Spring Batch 提供范围(stepjob)以允许后期绑定属性。如果要进行属性注入,则必须将 bean 定义为step范围(SimpleBatchlet不是null第一次运行消息的原因)。

那么在定义工作上下文时,如何实现 Spring DI 和基于标准的 JSR-352 之间的关注点分离?简单的。将作业 XML 导入 Spring XML 并将 Spring XML 名称传递JobOperator给要启动的。在你的情况下,而不是:

public static void main(String[] args) {

    System.setProperty("JSR-352-BASE-CONTEXT", "x98_batch_local.xml");

    Properties jobParameters = new Properties();
    jobParameters.put("message", "Hello!");

    JobOperator jobOperator = BatchRuntime.getJobOperator();
    jobOperator.start("x98SampleJob", jobParameters);

}

将以下内容添加到您的 x98_batch_local.xml:

<import resource="classpath*:x98SampleJob.xml"/>

然后通过以下方式执行您的工作:

public static void main(String[] args) {

    Properties jobParameters = new Properties();
    jobParameters.put("message", "Hello!");

    JobOperator jobOperator = BatchRuntime.getJobOperator();
    jobOperator.start("x98_batch_local.xml", jobParameters);

}

上述调整并将您设置SimpleBatchlet为步骤范围将导致您的工作按预期运行。

注意:由于这个问题,我创建了问题BATCH-2388以更好地记录上面讨论的上下文注意事项。

于 2015-05-29T16:59:00.450 回答