取消刷新尝试:org.springframework.beans.factory.BeanCreationException:创建名称为“ MyTastTasklet”的bean时出错

时间:2018-08-10 09:37:30

标签: spring spring-boot spring-batch

我正在开发 Spring Boot Batch Example 。在此示例中,我创建了具有实体,JPARepository和DB配置的BatchJPA核心模块。

这个模块作为依赖添加到另一个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();
    }
}

0 个答案:

没有答案