尝试运行Spring Batch代码时出现以下错误。我已经配置了JobRepository和JobLauncher,还配置了Job。通过main方法运行时出现以下错误
代码
<bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@XX.YY.INT:1527/TEST" />
<property name="username" value="TEST_USER" />
<property name="password" value="BGFFFF" />
</bean>
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="databaseType" value="ORACLE" />
</bean>
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
错误
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'jobLauncher' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:702)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1180)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1076)
at com.mastercard.ess.eds.SendCustomerApp.main(SendCustomerApp.java:27)
主要方法:
public class SendCustomerApp {
public static void main(String[] args) throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath*:/META-INF/spring/ABC_context.xml");
JobLauncher jobLauncher = (JobLauncher) applicationContext.getBean("jobLauncher");
Job job = (Job) applicationContext.getBean("ABCJob");
JobExecution execution = jobLauncher.run(job, new JobParameters());
System.out.println("EXIT STATUS :: "+execution.getStatus());
System.out.println("DONE !!");
}
}
ABC_Job.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@XX.YY.INT:1527/TEST" />
<property name="username" value="TEST_USER" />
<property name="password" value="BGFFFF" />
</bean>
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="databaseType" value="ORACLE" />
</bean>
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
<batch:job id="billDataBatchJob">
..........
..........
...........
.............
..............
</batch:job>
</beans>
我启用了日志并看到了以下日志
2018-06-25 15:16:25 DEBUG StandardEnvironment:127 - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
2018-06-25 15:16:25 DEBUG PathMatchingResourcePatternResolver:317 - Resolved classpath location [META-INF/spring/ABC.job.xml] to resources []
2018-06-25 15:16:25 DEBUG XmlBeanDefinitionReader:224 - Loaded 0 bean definitions from location pattern [classpath*:META-INF/spring/ABC.job.xml]
2018-06-25 15:16:25 DEBUG ClassPathXmlApplicationContext:615 - Bean factory for org.springframework.context.support.ClassPathXmlApplicationContext@51016012: org.springframework.beans.factory.support.DefaultListableBeanFactory@1a968a59: defining beans []; root of factory hierarchy
2018-06-25 15:16:25 DEBUG ClassPathXmlApplicationContext:728 - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@2286778]
2018-06-25 15:16:25 DEBUG ClassPathXmlApplicationContext:752 - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@3e9b1010]
2018-06-25 15:16:25 DEBUG DefaultListableBeanFactory:745 - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1a968a59: defining beans []; root of factory hierarchy
2018-06-25 15:16:25 DEBUG ClassPathXmlApplicationContext:779 - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@6ddf90b0]
2018-06-25 15:16:25 DEBUG DefaultListableBeanFactory:251 - Returning cached instance of singleton bean 'lifecycleProcessor'
2018-06-25 15:16:25 DEBUG PropertySourcesPropertyResolver:91 - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'jobLauncher' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:702)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1180)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1076)
at com.mastercard.ess.eds.CustomerFileGenerationMain.main(CustomerFileGenerationMain.java:17)
答案 0 :(得分:1)
我创建了一个项目来测试您的代码,一切顺利。我只是将dataSource bean属性更改为连接到h2-db而不是Oracle。关于您的问题,请考虑,您粘贴了ABC_Job.xml内容,但是在您的代码中,您引用了ABC_context.xml,也许这是您的问题。顺便说一句,如果添加适当的记录器并设置日志级别进行调试,则可以查看spring创建的bean,还可以跟踪bean以找出问题的原因。
答案 1 :(得分:1)
我能够通过使用FileSystemXMlApplication上下文解决以下问题。我坚信我正在使用的Maven项目的项目结构不正确,或者同时使用Boot和MainMethod。
public class MainApp {
public static void main(String[] args) {
PropertyConfigurator.configure("src/main/resources/log4j.properties");
String[] springConfig = { "C:\\XXXX\\src\\main\\resources\\META-INF\\spring\\XXXXX-job.xml" };
ApplicationContext context = new FileSystemXmlApplicationContext(springConfig);
JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
Job job = (Job) context.getBean("XXXJob");
try {
JobExecution execution = jobLauncher.run(job, new JobParameters());
System.out.println("Exit Status : " + execution.getStatus());
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Done");
}
}