Spring批处理Java配置@EnableBatchProcessing注释错误

Spring批处理Java配置@EnableBatchProcessing注释错误,java,spring,annotations,spring-batch,Java,Spring,Annotations,Spring Batch,我将SpringBatch 2.2与Spring3一起使用。我试图用Java配置而不是通常的XML配置来配置我的作业。下面是我的AppConfig类: @Configuration @EnableBatchProcessing public class AppConfig { @Autowired private JobBuilderFactory jobs; @Autowired private StepBuilderFactory steps; private Ta

我将SpringBatch 2.2与Spring3一起使用。我试图用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);

我做错了什么?有人有什么想法吗?

我建议为你的应用程序类添加注释:

@ComponentScan
@EnableAutoConfiguration
public class App {
...
第一个用于在预处理bean中查找配置,第二个用于批处理所需的关键bean。 在你的队伍中:

Job job = (Job) context.getBean("myJob");
请写在这里:

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

因为它是按类型自动连接的。

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

import org.springframework.context.annotation.Configuration;

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

我按照您的建议做了,但仍然无法工作。错误堆栈仍然指向ApplicationContext context=new AnnotationConfigApplicationContext(AppConfig.class);Line运行应用程序怎么样:ApplicationContext ctx=SpringApplication.run(App.class,args)?我选择了Spring引导路线以减少头痛,但后来我完全放弃了注释和Spring引导,选择了常用的XML配置。它的可定制性较差,但复杂性也较低。