首页 文章

Spring Batch上下文持有者null

提问于
浏览
0

抱歉淹没了 spring 批次SO队列 .

我从Job Scope模式开始,因为appears to not work in XML configuration for 3.0.6 . 我很可能只是感到困惑 .

我试着继续步骤范围,因为这似乎有效 . 这是我的bean的app-context.xml配置标头 . 这将启用 scope="step" 选项 .

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:batch="http://www.springframework.org/schema/batch"
   xmlns:util="http://www.springframework.org/schema/util"
   xmlns:p="http://www.springframework.org/schema/p"
   xsi:schemaLocation="
    http://www.springframework.org/schema/batch
    http://www.springframework.org/schema/batch/spring-batch-3.0.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util.xsd
    http://www.springframework.org/schema/cache
    http://www.springframework.org/schema/cache/spring-cache.xsd">

但是,创建这些bean失败了 . 重要的是,这些是REST Easy客户端代理bean . 我这样定义如下:

<bean id="rootServiceBean" scope="step"
      class="com.myorg.ServiceProxyFactoryBean"
      p:baseUri="${rest.base.uri}"
      p:ticket="#{jobExecutionContext['jobSecrets']}" abstract="true"/>

然后在我的实例上:

<bean id="someServiceObject"
      parent="rootServiceBean"
      p:serviceInterface="com.myOrg.someServiceRest"/>

但是我从我的顶级工作,子工作,子步骤和那些最终由...引起的步骤中得到错误 .

引起:org.springframework.beans.factory.BeanCreationException:在org.springframework.beans.factory.support的org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191)创建名为 'someService': Scope 'step' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No context holder available for step scope at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:341) 的bean时出错.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)... 79更多引起:java.lang.IllegalStateException:org.springframework.batch.core.scope.StepScope.getContext(StepScope.java)中没有可用于步骤范围的上下文持有者:167)org.springframework.batch.core.scope.StepScope.get(StepScope.java:99)org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:327)... 81更多

我需要在工作中初始化一些背景吗?或者 Spring 季批次是否与易用客户端正在利用的代理冲突?

1 回答

  • 1

    首先,你不应该因为一些假定的错误而放弃工作范围,更不用说“转移到步骤范围”;两者都很重要,有不同的用途 . 最明显的区别是作业范围的bean在作业持续时间内生存,并且在一个步骤的持续时间内使用步骤范围的bean .

    如果确实存在错误,您可以像注册任何其他Spring上下文一样注册作业范围;实际上,更简单,因为Spring Batch的范围实现还负责注册自身和自动创建单例代理bean(如果将 autoProxy 属性设置为 true ,这是默认值) . 您只需要包含 org.springframework.batch.core.scope.JobScope 类型的bean .

    至于你的错误,如果你有默认的autoproxying设置,你就不会有问题 . 请注意,在这种情况下,您指定的bean名称是给予singleton代理的bean名称,它委派给它的实际步骤作用域bean被重命名为 stepScopedTarget.<original bean name> ),或者,更有可能,如果你有基本工作,2)调用一个方法单身代理过早地,即在步骤激活之前 . 如果对同一作业使用多个线程,则可能还必须使用 StepSynchronizationManager::register / JobSynchronizationManager::close 将步骤范围传播给它 .

相关问题