spring - 取消刷新尝试: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();
}
}
解决方案
推荐阅读
- swift - 使用 UIImage(contentsOfFile: ) 时获取 EXC_BAD_ACCESS
- django - 在 django url dispatcher 中使用 Handler404 导致服务器错误
- load-balancing - 我需要更多资源还是需要负载均衡器?
- sql - 如何在子字符串 SQL 中搜索特殊字符
- asp.net - 为 ASP/ASPX 文件上传设置什么内容类型?
- android - 在 ListView 中添加 TextView 跑马灯效果失败
- mysql - 从 dbeaver 连接到 MySQL 服务器给出检查“useSSL”是否设置为“true”
- r - R 在尝试使用 registerDoParallel 注册集群后卡住了
- python - python中具有o(n)顺序的大列表的快速列表减法
- react-native - 无法解析模块`@expo/vector-icons/fonts/Ionicons.ttf`