在 Spring-boot 上将 Spring Batch 与 spring-batch-admin-manager 集成时出错

Posted

技术标签:

【中文标题】在 Spring-boot 上将 Spring Batch 与 spring-batch-admin-manager 集成时出错【英文标题】:Error Integrating Spring batch with spring-batch-admin-manager on Spring-boot 【发布时间】:2015-02-27 18:01:22 【问题描述】:

我正在尝试使用 spring-boot 嵌入式 tomcat 配置将 spring-batch-admin-manager 与 spring-boot-starter-batch 集成在一起。

但是我有这个我无法确定的错误跟踪:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'batchConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.batch.core.configuration.annotation.StepBuilderFactory com.mycompany.notification.processor.service.batch.configuration.BatchConfiguration.steps; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stepBuilders' defined in class path resource [org/springframework/batch/core/configuration/annotation/SimpleBatchConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.core.configuration.annotation.StepBuilderFactory]: Factory method 'stepBuilders' threw exception; nested exception is java.lang.ClassCastException: org.springframework.batch.core.repository.support.JobRepositoryFactoryBean$$EnhancerBySpringCGLIB$$6da887a9 cannot be cast to org.springframework.batch.core.repository.JobRepository
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    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:762)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    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:321)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:961)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:950)
    at com.mycompany.notification.processor.service.main.Application.main(Application.java:27)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.batch.core.configuration.annotation.StepBuilderFactory com.mycompany.notification.processor.service.batch.configuration.BatchConfiguration.steps; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stepBuilders' defined in class path resource [org/springframework/batch/core/configuration/annotation/SimpleBatchConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.core.configuration.annotation.StepBuilderFactory]: Factory method 'stepBuilders' threw exception; nested exception is java.lang.ClassCastException: org.springframework.batch.core.repository.support.JobRepositoryFactoryBean$$EnhancerBySpringCGLIB$$6da887a9 cannot be cast to org.springframework.batch.core.repository.JobRepository
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:558)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 21 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stepBuilders' defined in class path resource [org/springframework/batch/core/configuration/annotation/SimpleBatchConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.core.configuration.annotation.StepBuilderFactory]: Factory method 'stepBuilders' threw exception; nested exception is java.lang.ClassCastException: org.springframework.batch.core.repository.support.JobRepositoryFactoryBean$$EnhancerBySpringCGLIB$$6da887a9 cannot be cast to org.springframework.batch.core.repository.JobRepository
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:602)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1111)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1006)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    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.findAutowireCandidates(DefaultListableBeanFactory.java:1127)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1051)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:530)
    ... 23 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.core.configuration.annotation.StepBuilderFactory]: Factory method 'stepBuilders' threw exception; nested exception is java.lang.ClassCastException: org.springframework.batch.core.repository.support.JobRepositoryFactoryBean$$EnhancerBySpringCGLIB$$6da887a9 cannot be cast to org.springframework.batch.core.repository.JobRepository
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:591)
    ... 35 common frames omitted
Caused by: java.lang.ClassCastException: org.springframework.batch.core.repository.support.JobRepositoryFactoryBean$$EnhancerBySpringCGLIB$$6da887a9 cannot be cast to org.springframework.batch.core.repository.JobRepository
    at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$$EnhancerBySpringCGLIB$$607a70b7.jobRepository(<generated>)
    at org.springframework.batch.core.configuration.annotation.AbstractBatchConfiguration.stepBuilders(AbstractBatchConfiguration.java:63)
    at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$$EnhancerBySpringCGLIB$$607a70b7.CGLIB$stepBuilders$7(<generated>)
    at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$$EnhancerBySpringCGLIB$$607a70b7$$FastClassBySpringCGLIB$$ac8c0a6a.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:309)
    at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$$EnhancerBySpringCGLIB$$607a70b7.stepBuilders(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ... 36 common frames omitted

这是我的 gradle 构建文件:

buildscript 
    repositories 
        mavenCentral()
        maven  url 'http://repo.spring.io/milestone/' 
    
    dependencies 
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.0.RELEASE")
    


apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'

war 
    baseName = 'notification-processor-service'
    version = '1.0.0-SNAPSHOT'

jar 
    baseName = 'notification-processor-service'
    version = '1.0.0-SNAPSHOT'


ext 
    springIntegrationVersion = '4.1.1.RELEASE'
    springIntegrationKafkaVersion = '1.0.0.M2'


repositories 
    mavenCentral()
    maven 
        url 'https://repository.apache.org/content/groups/public'
    
    maven  url 'http://repo.spring.io/milestone/' 


dependencies 
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-batch:1.2.0.RELEASE")
    compile("org.springframework.boot:spring-boot-starter-actuator")
    compile("javax.inject:javax.inject:1")
    compile('org.springframework.batch:spring-batch-admin-manager:1.3.0.RELEASE') 
        exclude module: 'slf4j-log4j12'
    
    //SI
    // compile("org.springframework.integration:spring-integration-core:$springIntegrationVersion")

    /* //kafka
     compile("org.springframework.integration:spring-integration-kafka:$springIntegrationKafkaVersion") 
         exclude module: 'log4j'
         exclude module: 'jms'
         exclude module: 'jmxtools'
         exclude module: 'jmxri'
     */
    testCompile("org.springframework.boot:spring-boot-starter-test")



task wrapper(type: Wrapper) 
    gradleVersion = '1.11'

我添加了这些配置,如以下 github 示例所示:

https://github.com/codecentric/spring-batch-admin-spring-boot

@Configuration
@ImportResource("classpath:/org/springframework/batch/admin/web/resources/servlet-config.xml")
public class ServletConfiguration 


 */
@Configuration
@ImportResource("classpath:/org/springframework/batch/admin/web/resources/webapp-config.xml")
public class WebappConfiguration 


这是我的 batchConfiguration 类:

@EnableBatchProcessing
@Configuration
public class BatchConfiguration 

    private static final Logger logger = LoggerFactory.getLogger(BatchConfiguration.class);

    @Inject
    WriteToKafkaTasklet writeToKafkaTasklet;

    @Autowired
    private StepBuilderFactory steps;


    @Autowired
    private JobBuilderFactory jobs;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;



我错过了什么?

【问题讨论】:

您是否有示例代码在其中使用 spring-batch-admin 和 spring-batch?谢谢 【参考方案1】:

这里有几件事:

    不要在 Spring Batch Admin 中使用 @EnableBatchProcessing。 SBA 提供了许多开箱即用的组件。如果您愿意使用最新最好的,它提供了@EnableBatchProcessing 提供的所有内容,而无需使用注释。 您获得的堆栈跟踪是因为@EnableBatchProcessing 正在注册一个使用动态子类的StepScope,但Spring Batch Admin 也在注册一个使用java 代理的StepScope。如前所述,删除 @EnableBatchProcessing 应该会删除您的堆栈跟踪。

话虽如此,如果您正在使用当前发布的组件,一旦您删除了@EnableBatchProcessing,您将需要手动配置它提供的两个构建器。将以下 XML 添加到您的覆盖 XML 中,您应该可以做生意了:

<bean id="jobBuilderFactory" class="org.springframework.batch.core.configuration.annotation.JobBuilderFactory">
    <constructor-arg ref="jobRepository"/>
</bean>

<bean id="stepBuilderFactory" class="org.springframework.batch.core.configuration.annotation.StepBuilderFactory">
    <constructor-arg index="0" ref="jobRepository"/>
    <constructor-arg index="1" ref="transactionManager"/>
</bean>

请留意 Spring Batch Admin 2.0。这将包含这里讨论的所有内容。M1 应该会在接下来的几周内推出。

【讨论】:

你能看一下***.com/questions/27729750/…吗,谢谢。

以上是关于在 Spring-boot 上将 Spring Batch 与 spring-batch-admin-manager 集成时出错的主要内容,如果未能解决你的问题,请参考以下文章

spring-boot 无法在 docker 中启动

如何使用 Spring-Boot 播种 Spring-Security

在spring-boot启动期间tomcat数据源不检查有效连接?

Spring-Boot:用于重定向的 Web 过滤器

spring-boot 热部署 intellij IDE

在父 build.gradle 中找不到 ID 为 spring-boot 的插件