当前位置: 首页 > 知识库问答 >
问题:

有没有一种方法可以将spring-batch-admin和spring-boot适当地集成在一起?

黄俊智
2023-03-14

根据文档,spring batch admin非常容易嵌入到现有的应用程序中。简单地复制web.xml和index.jsp,然后添加所需的依赖项就足以使其工作。

但是如果我想在一个现有的spring boot项目中使用它,它会变得更糟。根据这个例子,这个配置有点蹩脚,但是它可以工作。直到我尝试在我的configuriton bean中使用@enableBatchProcessing注释。然后我得到以下异常。

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobBuilders' defined in class path resource [org/springframework/batch/core/configuration/annotation/SimpleBatchConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.batch.core.configuration.annotation.JobBuilderFactory org.springframework.batch.core.configuration.annotation.AbstractBatchConfiguration.jobBuilders() throws java.lang.Exception] threw exception; nested exception is java.lang.ClassCastException: org.springframework.batch.core.repository.support.JobRepositoryFactoryBean$$EnhancerBySpringCGLIB$$49fa0273 cannot be cast to org.springframework.batch.core.repository.JobRepository
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:597)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1095)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:990)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    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:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:706)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:952)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:941)
    at demo.Application.main(Application.java:35)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.batch.core.configuration.annotation.JobBuilderFactory org.springframework.batch.core.configuration.annotation.AbstractBatchConfiguration.jobBuilders() throws java.lang.Exception] threw exception; nested exception is java.lang.ClassCastException: org.springframework.batch.core.repository.support.JobRepositoryFactoryBean$$EnhancerBySpringCGLIB$$49fa0273 cannot be cast to org.springframework.batch.core.repository.JobRepository
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:188)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:586)
    ... 17 more
Caused by: java.lang.ClassCastException: org.springframework.batch.core.repository.support.JobRepositoryFactoryBean$$EnhancerBySpringCGLIB$$49fa0273 cannot be cast to org.springframework.batch.core.repository.JobRepository
    at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$$EnhancerBySpringCGLIB$$b5c6eb04.jobRepository(<generated>)
    at org.springframework.batch.core.configuration.annotation.AbstractBatchConfiguration.jobBuilders(AbstractBatchConfiguration.java:58)
    at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$$EnhancerBySpringCGLIB$$b5c6eb04.CGLIB$jobBuilders$8(<generated>)
    at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$$EnhancerBySpringCGLIB$$b5c6eb04$$FastClassBySpringCGLIB$$d88bd05f.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:312)
    at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$$EnhancerBySpringCGLIB$$b5c6eb04.jobBuilders(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166)
    ... 18 more

我的配置很简单我有两个配置bean

@Configuration
@ImportResource({"classpath:/org/springframework/batch/admin/web/resources/servlet-config.xml", 
        "classpath:/org/springframework/batch/admin/web/resources/webapp-config.xml"})
public class BatchAdminConfiguration {
}

而且

@Configuration
@EnableBatchProcessing
public class BatchImporterConfiguration { 
}

当我删除@EnableBatchProcessing并尝试使用JobBuilderFactory创建作业并使用@StepScope注释时,我会得到其他ClassCastExceptions。

现在我使用基于xml的配置来创建作业、步骤和其他bean。它工作得很好,但实际上我更喜欢xml自由配置。有没有一种方法可以轻松地集成spring boot、spring batch和spring batch admin?

共有1个答案

虞博涛
2023-03-14

简单的回答是,您不希望在Spring批处理管理中使用@enableBatchProcessing。SBA在全局范围内提供了许多bean,@enableBatchProcessing也提供了这些bean。SBA 2.0(目前正在开发中)可能会填补当前与@EnableBatchProcessing提供的(特别是提供JobBuilderFactoryStepBuilderFactory)之间的空白。

要使自己运行起来,您应该能够在meta-inf/spring/batch/override/目录中配置一个jobBuilderFactory和一个stepBuilderFactory以供全局使用(我自己还没有厌倦)。从那里,您可以使用meta-inf/spring/batch/jobs目录中的XML文件,这些文件只对@configuration类执行组件扫描。但是,由于bean的重复,请不要使用@enableBatchProcessing

需要说明的是,这不是一个Spring Boot问题,因为@enablebatchprocessing是一个Spring批处理注释,而不是一个引导注释。

 类似资料:
  • 问题内容: 根据文档, spring batch admin非常容易嵌入到现有应用程序中。只需复制web.xml和index.jsp,然后添加所需的依赖项就足以使其正常工作。 但是,如果我想在现有的Spring Boot项目中使用它,那就越来越糟了。根据此示例,配置有些麻烦,但是可以使用。直到我尝试在我的configuriton bean中使用注释。然后我得到以下异常。 我的配置非常简单,我有两个

  • 我使用spring-batch和Spring-Boot完成了一个项目。 业务模式MySQL 和修改的application.properties: 这是副作用。除了java配置之外,我的应用程序还使用了applicationContext.xml。

  • 问题内容: 我的网页上有一个“瘦”列表:例如,一个包含100个项目的列表,每个项目的长度为一个单词。为了减少滚动,我想在页面的两列甚至四列中显示此列表。我该如何使用CSS? 我希望该解决方案具有灵活性,这样,如果列表增加到200个项目,则无需进行很多手动调整即可容纳新列表。 问题答案: ul { -moz-column-count: 4; -moz-column-gap: 20px; -webki

  • 我有一个Spring批处理项目,它使用基于注释的配置,并且是一个工作代码本身。现在我想将它与一个新的Spring Batch管理项目集成。我尝试了一些博客中可用/回答的解决方案,比如将批处理项目的依赖项添加到批处理管理项目中,并修改META-INF/spring/batch/servlet/override/context-config.xml文件以指向批处理项目配置文件。即。job包存在于Spr

  • 我正在用SpringBoot构建一个应用程序。我有一个存储库,我通过以下方式从方法名称创建查询: 这样的查询可以工作,方法调用的结果是具有该名称的用户。在Spring Boot中是否可以按名称创建查询,其中输入是名称列表,输出是用户列表,其中每个用户至少对应于列表中的一个名称?例如: 我也试过这样的方法,但是行不通。

  • 我想在我的spring web应用程序中添加几个过滤器,但至少现在不会有任何关于安全性的内容。所以。没有spring-security我所能做的就是在web.xml中定义多个过滤器(定义过滤器的旧方法)。要能够使用spring过滤器链,我需要为我的项目添加spring-security作为依赖项,这似乎很奇怪。也许我做错了什么,而且确实有过滤器链可以在没有spring-security依赖的情况下