spring批次NoSuchBeanDefinitionException
spring batch NoSuchBeanDefinitionException
我开发了一个 spring 批处理来将用户加载到数据库中。我能够 运行 它作为一个来自 eclipse 的 Java 应用程序提供主要 class 但不能 运行 通过 unix 上的 shell 脚本批处理服务器。我得到这个例外:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'jobLauncher' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:529)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1095)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:277)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1093)
at com.batch.LoadBatch.launchJob(LoadBatch.java:56)
at com.batch.LoadBatch.main(LoadBatch.java:38)
我在 job.xml 文件中声明了我的 bean,如下所示
<?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"
xmlns:batch="http://www.springframework.org/schema/batch"
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.1.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd"">
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:LoadBatch.properties </value>
</property>
</bean>
<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>
<!--
*****************************productLoadJob********************************
-->
<bean id="completionPolicy" class="org.springframework.batch.repeat.policy.DefaultResultCompletionPolicy"/>
<bean id="multiResourceReader"
class=" org.springframework.batch.item.file.MultiResourceItemReader" scope="step">
<property name="resources" value="file:///${inputFilePath}*.csv" />
<property name="delegate" ref="flatFileItemReader" />
</bean>
<bean id="flatFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="fieldSetMapper">
<bean class="com.mapper.IndividualRetailerFieldSetMapper" />
</property>
<property name="lineTokenizer">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<property name="delimiter" value="," />
</bean>
</property>
</bean>
</property>
<property name="linesToSkip" value="1" />
</bean>
<bean id="flatFileItemWriter"
class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
<property name="resource" value="file:${outputFilePath}${outputFileName}.${outputFileType}" />
<property name="appendAllowed" value="true" />
<property name="lineAggregator">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
<property name="delimiter" value="," />
<property name="fieldExtractor">
<bean
class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<property name="names" value="serialNo,mobileNo,role,parentId" />
</bean>
</property>
</bean>
</property>
</bean>
<bean id="itemProcessor" class="com.handlers.LoadWriter" scope="step">
<property name="LoadFileHandler" ref="filehandler" />
<property name="LoadFieldSetMapper" ref="LoadFieldSetMapper"/>
<property name="opId" value="ABC" />
<property name="buId" value="XYZ" />
</bean>
<bean id="batchExceptionListner" class="com.handlers.LoadBatchExceptionListener" >
<property name="irLoadFileHandler" ref="filehandler" />
</bean>
<bean id="filehandler" class="com.handlers.IRLoadFileHandler" />
<bean id="IRLoadFieldSetMapper" class="com.handlers.IRLoadFieldSetMapper" />
<bean id="skipExceptionListner" class="com.handlers.IRLoadSkipExceptionListener" />
<batch:job id="IRLoadJob">
<batch:step id="importFileStep">
<batch:tasklet>
<batch:chunk reader="multiResourceReader" writer="flatFileItemWriter"
processor="itemProcessor" commit-interval="1">
</batch:chunk>
</batch:tasklet>
</batch:step>
<batch:listeners>
<batch:listener ref="batchExceptionListner" />
</batch:listeners>
</batch:job>
</beans>
在此处添加上下文初始化代码:
context = new ClassPathXmlApplicationContext(new String[] {"classpath*:Job.xml"});
logger.info("context in Spring Initializer is :: " + context.getBeanDefinitionCount());
我在 2 个位置拥有属性和 xml 文件,即在我的批处理 jar 中以及在服务器上配置批处理的位置。我从 jar 中删除了文件,我的批处理开始了 运行! :)
我开发了一个 spring 批处理来将用户加载到数据库中。我能够 运行 它作为一个来自 eclipse 的 Java 应用程序提供主要 class 但不能 运行 通过 unix 上的 shell 脚本批处理服务器。我得到这个例外:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'jobLauncher' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:529)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1095)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:277)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1093)
at com.batch.LoadBatch.launchJob(LoadBatch.java:56)
at com.batch.LoadBatch.main(LoadBatch.java:38)
我在 job.xml 文件中声明了我的 bean,如下所示
<?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"
xmlns:batch="http://www.springframework.org/schema/batch"
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.1.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd"">
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:LoadBatch.properties </value>
</property>
</bean>
<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>
<!--
*****************************productLoadJob********************************
-->
<bean id="completionPolicy" class="org.springframework.batch.repeat.policy.DefaultResultCompletionPolicy"/>
<bean id="multiResourceReader"
class=" org.springframework.batch.item.file.MultiResourceItemReader" scope="step">
<property name="resources" value="file:///${inputFilePath}*.csv" />
<property name="delegate" ref="flatFileItemReader" />
</bean>
<bean id="flatFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="fieldSetMapper">
<bean class="com.mapper.IndividualRetailerFieldSetMapper" />
</property>
<property name="lineTokenizer">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<property name="delimiter" value="," />
</bean>
</property>
</bean>
</property>
<property name="linesToSkip" value="1" />
</bean>
<bean id="flatFileItemWriter"
class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
<property name="resource" value="file:${outputFilePath}${outputFileName}.${outputFileType}" />
<property name="appendAllowed" value="true" />
<property name="lineAggregator">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
<property name="delimiter" value="," />
<property name="fieldExtractor">
<bean
class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<property name="names" value="serialNo,mobileNo,role,parentId" />
</bean>
</property>
</bean>
</property>
</bean>
<bean id="itemProcessor" class="com.handlers.LoadWriter" scope="step">
<property name="LoadFileHandler" ref="filehandler" />
<property name="LoadFieldSetMapper" ref="LoadFieldSetMapper"/>
<property name="opId" value="ABC" />
<property name="buId" value="XYZ" />
</bean>
<bean id="batchExceptionListner" class="com.handlers.LoadBatchExceptionListener" >
<property name="irLoadFileHandler" ref="filehandler" />
</bean>
<bean id="filehandler" class="com.handlers.IRLoadFileHandler" />
<bean id="IRLoadFieldSetMapper" class="com.handlers.IRLoadFieldSetMapper" />
<bean id="skipExceptionListner" class="com.handlers.IRLoadSkipExceptionListener" />
<batch:job id="IRLoadJob">
<batch:step id="importFileStep">
<batch:tasklet>
<batch:chunk reader="multiResourceReader" writer="flatFileItemWriter"
processor="itemProcessor" commit-interval="1">
</batch:chunk>
</batch:tasklet>
</batch:step>
<batch:listeners>
<batch:listener ref="batchExceptionListner" />
</batch:listeners>
</batch:job>
</beans>
在此处添加上下文初始化代码:
context = new ClassPathXmlApplicationContext(new String[] {"classpath*:Job.xml"});
logger.info("context in Spring Initializer is :: " + context.getBeanDefinitionCount());
我在 2 个位置拥有属性和 xml 文件,即在我的批处理 jar 中以及在服务器上配置批处理的位置。我从 jar 中删除了文件,我的批处理开始了 运行! :)