找不到匹配的工厂方法:工厂bean;工厂方法'configurer()'。检查具有指定名称的方法是否存在并且该方法是非静态的

时间:2018-08-11 19:37:26

标签: spring spring-boot spring-batch

我正在开发基于Spring Boot Batch XML的方法。在此示例中,我开发了一个CommonConfig,如下所示。我想以某种方式针对Spring Batch使用基于XML的方法,而不是基于注释的方法。

CommonConfig.java

@StepScope
@Configuration
@ComponentScan({ "com.XXXX" })
@EnableBatchProcessing
@ImportResource({"classpath:jobs/ABC.xml"})
public class CommonConfig {

    @Bean
    BatchConfigurer configurer(
            @Qualifier("dataSource") DataSource dataSource) {
        return new DefaultBatchConfigurer(dataSource);
    }

    @Bean
    public JobBuilderFactory jobBuilderFactory(JobRepository jobRepository) {
        return new JobBuilderFactory(jobRepository);
    }

    @Bean
    public StepBuilderFactory stepBuilderFactory(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
        return new StepBuilderFactory(jobRepository, transactionManager);
    }
}

错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configurer' defined in class path resource [com//config/CommonConfig.class]: No matching factory method found: factory bean 'commonConfig'; factory method 'configurer()'. Check that a method with the specified name exists and that it is non-static.
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:547) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1247) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1096) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:535) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:398) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:330) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1258) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
    at com.jpaApplication.main(jpaApplication.java:18) [classes/:na]

2 个答案:

答案 0 :(得分:2)

我为此苦苦挣扎了大约1/2个小时,最后将我的班级从“ Config”重命名为“ Configurations”,并且它成功了。也许某处还有另一个“ Config”?谁知道。

答案 1 :(得分:0)

EnableBatchProcessing批注将自动将JobBuilderFactoryStepBuilderFactory(以及其他bean)添加到您的应用程序上下文中(请参见此处的Javadoc:https://docs.spring.io/spring-batch/4.0.x/api/org/springframework/batch/core/configuration/annotation/EnableBatchProcessing.html)。因此,您不需要自己定义这些bean。您需要做的是定义您的数据源,并将由DefaultBatchConfigurer提取:

@Configuration
@ComponentScan({ "com.XXXX" })
@EnableBatchProcessing
@ImportResource({"classpath:jobs/ABC.xml"})
public class CommonConfig {

   @Bean
   public DataSource dataSource() {
      // return your data source
   }


}

我建议您遵循入门指南https://spring.io/guides/gs/batch-processing/,了解如何使用Spring Boot正确配置和运行Spring Batch作业。