开发基于 Spring Boot JPA Batch XML 的方法。我开发了一些代码,它开始给我下面的错误。
有人可以指导我解决这个错误吗?
理想情况下,我期望应创建JobLauncher和JobRepository实例以供应用程序使用。在这里还需要配置什么?
错误:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field studentTestJob in com.its.example.service.HelloWorldService required a bean of type 'org.springframework.boot.autoconfigure.batch.BatchProperties$Job' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.boot.autoconfigure.batch.BatchProperties$Job' in your configuration.
H2Application.java
@SpringBootApplication
@EnableBatchProcessing
@ImportResource("classpath:applicationContext.xml")
@Import(AdditionalBatchConfiguration.class)
public class H2Application implements CommandLineRunner {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
StudentRepository repository;
@Autowired
HelloWorldService helloWorldService;
public static void main(String[] args) {
SpringApplication.run(H2Application.class, args);
}
@Override
public void run(String... args) throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(H2Application.class);
..........
.........
}
}
HelloWorldService.java
@Service
public class HelloWorldService {
private Logger logger = LogManager.getLogger(HelloWorldService.class.getName());
@Autowired
JobLauncher jobLauncher;
@Autowired
JobBuilderFactory jobBuilderFactory;
@Autowired
Job studentTestJob;
public void testMethod() {
logger.info(" test method in the service called");
Map<String,String> testmsg = new HashMap<>();
testmsg.put("KEY1", "value 1");
testmsg.put("KEY2", "value 2");
logger.info(testmsg);
JobParameters jobParameters = new JobParametersBuilder().toJobParameters();
}
}
testjob.xml
<batch:job id="StudentTestJob">
<batch:step id="step1">
<batch:tasklet>
<batch:chunk reader="studentReader" writer="studentWriter"
processor="studentProcessor" commit-interval="10">
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<context:property-placeholder/>
<import resource="testjob.xml"/>
<bean id="helloService" class="com.its.example.service.HelloWorldService"/>
</beans>