Spring Batch在Web应用程序中找不到全局单例作用域范围的Bean-有什么问题?

时间:2019-04-01 18:25:39

标签: spring spring-batch

我有一个spring Web应用程序,该应用程序注册了多个spring batch作业,以便在启动时进行长时间的后台处理。据我所知,spring-batch上下文不知道在根AnnotationConfigWebApplicationContext bean中声明的单例,因此在初始应用程序加载时以及在初始应用程序加载时都为每个作业实例化了多个复杂初始化bean的副本。执行时间。

我已经确定在AbstractBeanFactory.doGetBean()中将Bean正确标识为单例,但是由于某种原因,用于作业的Bean工厂没有意识到用于父上下文的Bean工厂。

我已经将一些bean重构为Application范围的,但是在春季批处理上下文中,(显然)应用范围Bean是不合法的。

或者我对弹簧作用域有一些误解,或者我对spring-batch元素的初始化有些失误(下面的代码)。我倾向于两者,因为一个会导致另一个。

据我了解Spring范围,我应该看到类似的内容,每个子范围都能够看到在父范围中定义的单例:

AnnotationConfigWebApplicationContext (web application context)
        |
        v
ResourceXmlApplicationContext (1 per registered job)
        |
        v
ResourceXmlApplicationContext (1 per registered step)

有问题的初始化代码:

@Component("mySingletonScopedBean")
@Scope(value = "singleton", proxyMode = ScopedProxyMode.DEFAULT)
@Order(1)
public class MySingletonScopedBean {
    // getters, setters, etcetera
}


// EDIT: Added in response to comment below
@Autowired
public ApplicationContext applicationContext;

@Bean
public ClasspathXmlApplicationContextsFactoryBean classpathXmlApplicationContextsFactoryBean () throws IOException
{
    String resourcePath = somePath "*.xml";

    logger.trace("classpathXmlApplicationContextsFactoryBean()  :: {} ", resourcePath);
    Resource[] resources = applicationContext.getResources(resourcePath);


    ClasspathXmlApplicationContextsFactoryBean bean = new ClasspathXmlApplicationContextsFactoryBean ();
    bean.setApplicationContext(applicationContext);
    bean.setResources(resources);

    return bean;
}

@Bean
public AutomaticJobRegistrar automaticJobRegistrar() throws IOException, Exception {

    ClasspathXmlApplicationContextsFactoryBean c = classpathXmlApplicationContextsFactoryBean ();

    AutomaticJobRegistrar automaticJobRegistrar = new AutomaticJobRegistrar();
    automaticJobRegistrar.setApplicationContext(applicationContext);
    automaticJobRegistrar.setApplicationContextFactories(c.getObject());
    automaticJobRegistrar.setJobLoader(jobLoader());

    return automaticJobRegistrar;
}

@Bean
public JobLoader jobLoader() {
    DefaultJobLoader jobLoader = new DefaultJobLoader(jobRegistry(), stepRegistry());
    return jobLoader;
}

@Bean
public StepRegistry stepRegistry() {
    MapStepRegistry stepRegistry = new MapStepRegistry();
    return stepRegistry;
}

@Bean
public JobRegistry jobRegistry() {
    JobRegistry jobRegistry = new MapJobRegistry();
    return jobRegistry;
}

编辑:关闭

整个Spring环境的初始化都搞砸了。

我要省掉关于“干净”注入模型的怨言,要弄清在该项目结束后因大量博客文章而中断工作是一件多么痛苦的事情。

0 个答案:

没有答案