我收到以下错误:
org.springframework.beans.factory.BeanCreationException创建类路径资源中定义名为'job'的bean时出错 . 通过工厂方法进行Bean实例化失败 . 无法实例化[org.springframework.batch.core.Job]:工厂方法'job'抛出异常com / java / BatchConfig.class
在尝试编译以下代码时:
@Configuration
@EnableBatchProcessing
@EnableJpaRepositories(basePackageClasses = { TestRepository.class })
public class BatchConfig {
@Autowired
EntityManager entityManager;
@Autowired
private JobBuilderFactory jobs;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
public Job job() {
return jobs.get("job")
.start(step1())
.build();
}
@Bean
public Step step1(){
return stepBuilderFactory.get("step1")
.<testPojo,testPojo>chunk(5)
.reader(xmlFileItemReader())
.processor(itemProcessor())
.writer(ItemWriter())
.build();
}
@Bean
public ItemReader<testPojo> xmlFileItemReader() {
StaxEventItemReader<testPojo> xmlFileReader = new
StaxEventItemReader<>();
xmlFileReader.setResource(new FileSystemResource("C:\\*.xml"));
xmlFileReader.setFragmentRootElementName("document");
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(testPojo.class);
marshaller.setMappedClass(testPojo.class);
xmlFileReader.setUnmarshaller(marshaller);
return xmlFileReader;
}
@Bean
public ItemProcessor itemProcessor() {
return new TestProcessor();
}
@Bean
public JpaItemWriter<testDTO> customerItemWriter()
{
JpaItemWriter<testDTO> writer = new JpaItemWriter<>();
writer.setEntityManagerFactory(entityManager.getEntityManagerFactory());
return writer;
}
}
TestProcessor.java
public class TestProcessor implements ItemProcessor<TestPojo, TestDTO> {
private static final Logger log =
LoggerFactory.getLogger(TestProcessor.class);
@Override
public TestDTO process(final TestPojo npojo) throws Exception {
final TestDTO sndto = new TestDTO();
ndto.setMaster_object_name(npojo.getMaster_object());
ndto.setFull_title(npojo.getFull_title());
ndto.setAction_expiry_date(npojo.getAction_expiry());
ndto.setAction_objective(npojo.getAction_objective());
ndto.setRecommended_action(npojo.getRecommended_action());
return ndto;
}
}
Spring Boot class:
@SpringBootApplication
@EnableBatchProcessing
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
Pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>