首页 > 解决方案 > 取消刷新尝试:org.springframework.beans.factory.BeanCreationException:创建名为“MyTastTasklet”的bean时出错

问题描述

我正在开发Spring Boot Batch Example。在这个例子中,我创建了 BatchJPA 核心模块,它具有实体、JPARepository 和 DB 配置。

该模块作为依赖项添加到另一个 Spring 模块中,在此模块中,我正在添加与代码相关的特定批处理作业(如自定义存储库等)。我总共有 15 个批处理作业,我将使用 BatchJPA 依赖项创建单独的 Spring Boot 项目。

10-08-2018 14:54:11.853 [main] WARN  org.springframework.context.support.ClassPathXmlApplicationContext.refresh - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyTestTasklet': Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'transactionManager' available
10-08-2018 14:54:11.855 [main] INFO  org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener.logAutoConfigurationReport - 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
10-08-2018 14:54:11.919 [main] ERROR org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter.report - 

***************************
APPLICATION FAILED TO START
***************************

Description:

A component required a bean named 'transactionManager' that could not be found.


Action:

Consider defining a bean named 'transactionManager' in your configuration.

下面的代码:

@Service
public class MyTaskTasklet implements Tasklet {

    @Autowired
    private MyCustomerCustomRepository myCustomerCustomRepository;

    @Override
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {

        List<MyTest> mydata = myCustomerCustomRepository.getElligibleData();
        if (!mydata.isEmpty()) {
            System.out.println("XXXXXX = " + mydata.size());
        }

        chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext()
                .put("listOfData", mydata);

        return RepeatStatus.FINISHED;
    }
}

另一个文件:

@Component
@EnableBatchProcessing
public class MyJobLauncher {

    public void executeJob() {
        String[] springConfig = { "jobs/ABC.xml"};

        ApplicationContext context = new ClassPathXmlApplicationContext(springConfig);

        JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
        Job job = (Job) context.getBean("MyJobId");

        try {
            JobParameters jobParameters = new JobParametersBuilder().addString("runMode", "DATA")
                    .addDate("date", new Date()).addLong("time", System.currentTimeMillis()).toJobParameters();
            jobLauncher.run(job, jobParameters);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

和另一个文件 MainApplication

@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
public class MainApplication implements CommandLineRunner {

    @Autowired
    private MyJobLauncher jobLauncher;

    public static void main(String[] args) {
        SpringApplication.run(MyJobLauncher.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        jobLauncher.executeJob();
    }
}

标签: springspring-bootspring-batch

解决方案


推荐阅读