我尝试注入多个上下文来运行我的应用程序,但服务器崩溃了,我收到了以下日志错误:
'Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.batch.core.configuration.ListableJobLocator] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1100)'
我检查了错误,我找到了一些详细信息here,但我在下面的代码中找不到错误:
要加载的上下文:
@ComponentScan
@EnableAutoConfiguration
@EnableJpaRepositories
@ImportResource("classpath:job-report.xml")
public class DBDataManipulatorServiceContext {
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().setName("aircraft").setType(H2).build();
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
lef.setDataSource(dataSource);
lef.setJpaVendorAdapter(jpaVendorAdapter);
lef.setPackagesToScan("com");
return lef;
}
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
hibernateJpaVendorAdapter.setShowSql(true);
hibernateJpaVendorAdapter.setGenerateDdl(true);
hibernateJpaVendorAdapter.setDatabase(Database.H2);
return hibernateJpaVendorAdapter;
}
@Bean
public PlatformTransactionManager transactionManager() {
return new JpaTransactionManager();
}
}
要导入的xml文件:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd
">
<context:component-scan base-package="com" />
<!-- stored job-meta in memory -->
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
<property name="transactionManager" ref="transactionManager" />
</bean>
<bean id="transactionManager"
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
<bean id="Aircraft" class="com.domain.Aircraft" scope="prototype" />
<bean id="customReader" class="com.batch.CustomReader" />
<bean id="customWriter" class="com.batch.CustomWriter" />
<batch:job id="reportJob">
<batch:step id="step1">
<batch:tasklet>
<batch:chunk reader="customReader" writer="customWriter"
commit-interval="10">
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job>
<bean id="runScheduler" class="com.RunScheduler" />
<!-- Run every Hour -->
<task:scheduled-tasks>
<!-- <task:scheduled ref="runScheduler" method="run" fixed-delay="3600000"
/> -->
<task:scheduled ref="runScheduler" method="run"
cron="0 0 * * * *" />
</task:scheduled-tasks>
runScheduler是:
@Component
public class RunScheduler {
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job job;
public void run() {
try {
String dateParam = new Date().toString();
JobParameters param = new JobParametersBuilder().addString("date", dateParam).toJobParameters();
JobExecution execution = jobLauncher.run(job, param);
System.out.println("Exit Status : " + execution.getStatus());
} catch (Exception e) {
e.printStackTrace();
}
}
}
我的自定义阅读器:
public class CustomReader implements ItemReader<Map<Integer, Aircraft>> {
@Override
public Map<Integer, Aircraft> read() throws Exception, UnexpectedInputException,
ParseException, NonTransientResourceException {
HTMLDataExtractorService htmlDataExtractor = new HTMLDataExtractorService();
Map<Integer, Aircraft> aircraftsMap = htmlDataExtractor.parsePage("/Sabri/DTDailyReport.html");
return aircraftsMap;
}
}
和我的定制作者: 公共类CustomWriter实现ItemWriter {
@Override
public void write(List<? extends Aircraft> aircrafts) throws Exception {
DBDataManipulatorServiceImpl dbDataManipulatorImpl = new DBDataManipulatorServiceImpl();
Map <Integer, Aircraft> aircraftsMap = null;
for(Aircraft aircraft: aircrafts){
aircraftsMap.put(aircraft.getAircraftId(), aircraft);
}
dbDataManipulatorImpl.saveToDataBase(aircraftsMap);
}}
抱歉没有任何不便,但有人能告诉我我的错误在哪里,我该如何解决这个问题?
答案 0 :(得分:1)
在ApplicationContext.xml中添加以下条目,其中声明了jobLauncher和jobRegistry bean解决了我的问题
<bean id="jobRegistry"
class="org.springframework.batch.core.configuration.support.MapJobRegistry" />
答案 1 :(得分:0)
将@EnableBatchProcessing添加到您的配置中,这大致相当于使用XML命名空间