[英]What is the correct way to scope spring-batch components in an annotation driven configuration?
我正在圍繞Spring MVC構建一個包含spring-batch的應用程序,該應用程序以類似於spring-batch-admin的方式動態管理批處理過程。 我正在嘗試使用spring-batch-admin作為基礎來了解正在發生的事情,將上下文初始化轉換為注釋驅動的配置。
當應用程序盡可能使用注釋驅動的配置時,spring-batch作業將外部化為XML文件。
在啟動時,當嘗試刷新上下文時,正確初始化ClasspathXmlApplicationContextsFactoryBean后,AppInitializer失敗。 我知道該bean已正確初始化,因為它試圖加載作業之一,並且找不到步進作用域的bean。 (該過程不應加載並執行該作業,它僅應能夠在此時找到它們)。
這些作業本身在spring-batch-admin下可以正常工作,因此這些作業沒有問題。
我敢肯定,我只是誤解了不同部分的上下文,而解決方案取決於使這些部分進入正確的上下文。 誰能指出我錯過了什么?
環境:
Java 1.8
Spring 4.1.6
Spring-batch 3.0.2
Pivotal TC Developer Edition v3.0
提前致謝
日志摘錄:
DEBUG DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@425cccc3: defining beans [org.springframework.batch.core.scope.JobScope#0,org.springframework.batch.core.scope.StepScope#0,org.springframework.beans.factory.config.CustomEditorConfigurer,org.springframework.batch.core.configuration.xml.CoreNamespacePostProcessor,batch_state_transition_comparator,step0002-fetch,step0003-archive-purge,org.springframework.batch.core.configuration.xml.SimpleFlowFactoryBean#0,archive-purge-mt]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@51ae827
WARN AnnotationConfigWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'step0002-fetch': Cannot resolve reference to bean 'sourceSelectionReader' while setting bean property 'itemReader'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sourceSelectionReader' is defined
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
...
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sourceSelectionReader' is defined
at
...
sourceSelectionReader Bean
@Component("sourceSelectionReader")
@Scope(value = "step", proxyMode = ScopedProxyMode.DEFAULT)
public class SourceSelectionReaderImpl implements ItemReader<TreeModel>,
ApplicationContextAware, StepExecutionListener, SourceSelectionReader {
AppInitializer:
public class AppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) throws ServletException {
Logger logger = LoggerFactory.getLogger(this.getClass());
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(MyConfig.class);
rootContext.refresh();
// Manage the lifecycle of the root appcontext
container.addListener(new ContextLoaderListener(rootContext));
container.setInitParameter("defaultHtmlEscape", "true");
AnnotationConfigWebApplicationContext jobExecutionContext = new AnnotationConfigWebApplicationContext();
jobExecutionContext.setParent(rootContext);
jobExecutionContext.register(ExecutionContextConfig.class);
jobExecutionContext.refresh();
AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext();
mvcContext.register(ViewConfig.class);
mvcContext.setServletContext(container);
... (snip) ...
}
}
我相信您應該使用@Scope(value="step", proxyMode=ScopedProxyMode.TARGET_CLASS)
或其快捷方式@StepScope
。
PICNIC錯誤:我使用了錯誤的范例。 通過更深入的了解,我發現我嘗試使用的JobRegistry組件意味着直接從類路徑加載和執行。 我已經使用DefaultBatchConfigurer進行了重構,然后轉到下一個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.