Spring Batch Java Config @EnableBatchProcessing Annotation Error

时间:2014-12-12 10:33:39

标签: java spring annotations spring-batch

我在Spring 3中使用Spring Batch 2.2。我试图用Java配置而不是通常的XML配置来配置我的工作。下面是我的AppConfig类:

@Configuration
@EnableBatchProcessing
public class AppConfig {

    @Autowired private JobBuilderFactory jobs;
    @Autowired private StepBuilderFactory steps;
    private Tasklet simpleTasklet = new SimpleTasklet();

    @Bean
    public Job job() {
        return jobs.get("myJob").start(step1(simpleTasklet)).build();
    }

    @Bean protected Step step1(Tasklet tasklet) {
        return steps.get("step1").tasklet(tasklet).build();
    }

}

使用以下课程运行它:

public class App {
    public static void main(String[] args) {

        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        JobLauncher jobLauncher = (JobLauncher)context.getBean("jobLauncher");
        Job job = (Job) context.getBean("myJob");
        try{
            JobExecution execution = jobLauncher.run(job, new JobParameters());
            System.out.println("Exit status : " + execution.getStatus());
        } catch (Exception e){
            e.printStackTrace();
        } 
    }

}

产生此错误:

Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: org.springframework.batch.core.configuration.annotation.BatchConfigurationSelector was imported as a @Configuration class but was not actually annotated with @Configuration. Annotate the class or do not attempt to process it.
Offending resource: class path resource [org/springframework/batch/core/configuration/annotation/BatchConfigurationSelector.class]

并且错误堆栈指向此行:

ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

我做错了什么?任何想法?

2 个答案:

答案 0 :(得分:0)

我建议您注释您的App类:

@ComponentScan
@EnableAutoConfiguration
public class App {
...

第一个用于在预处理Bean中查找配置,第二个用于批处理所需的关键Bean。 在你的行:

Job job = (Job) context.getBean("myJob");

请写在这里:

Job job = context.getBean(Job.class);

因为它按类型自动装配。

答案 1 :(得分:0)

看起来@Configuration的导入是错误的。它应该是......

import org.springframework.context.annotation.Configuration;

如果不是这样的话,我建议使用Spring Boot来启动应用程序,这样你就可以确定它的接线正确了。