当前位置: 首页 > 知识库问答 >
问题:

无法使用Spring批处理框架连接到Oracle数据源

费子濯
2023-03-14

创建名为“step1”的bean时出错:设置bean属性“job repository”时无法解析对bean“job repository”的引用;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建类路径资源[spring/batch/config/spring-batch-contextoriginal.xml]中定义的名为“作业存储库”的bean时出错:设置属性值时出错;嵌套异常为org.springframework.beans.NotWritablePropertyException:bean类[org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean]的属性“data source”无效:bean属性“data source”不可写或具有无效的setter方法。setter的参数类型是否与getter的返回类型匹配?

我想我把所有东西都放了,但我认为对于这个错误最重要的是:spring-batch-context.xmlspring-datasource.xml。是我错过了什么还是有什么不对劲?如果你需要更多的细节请告诉我。谢谢你。(我在没有使用数据库的情况下尝试了这个示例,它工作得很好。)

spring-batch-context.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" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans   
                           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

 <import resource="../jobs/jobPerson.xml"/>
  <import resource="../config/spring-datasource.xml" /> 

   <!--  <context:annotation-config />
    <tx:annotation-driven transaction-manager="transactionManager"/>a PlatformTransactionManager is still required
     -->

    <!-- JobRepository and JobLauncher are configuration/setup classes -->
    <bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean" >
       <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>


    <!-- Optional ItemProcessor to perform business logic/filtering on the input records -->
    <bean id="itemProcessor" class="springBatch.ExamResultItemProcessor" />

    <!-- Optional JobExecutionListener to perform business logic before and after the job -->
    <bean id="jobListener" class="springBatch.ExamResultJobListener" />

    <!-- Step will need a transaction manager -->
    <bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />

</beans>
 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans    
                           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   
                           http://www.springframework.org/schema/jdbc    
                           http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd">

   <!-- Info to connect To Database -->
   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
      <property name="url" value="jdbc:oracle:thin:@*********:1552/******" />     
      <property name="username" value="*******" />
      <property name="password" value="*******" />
   </bean>

   <bean id="transactionManager"
        class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />

    <!-- create job-meta tables automatically -->
    <jdbc:initialize-database data-source="dataSource">
        <jdbc:script location="org/springframework/batch/core/schema-drop-oracle10g.sql" />
        <jdbc:script location="org/springframework/batch/core/schema-oracle10g.sql" />
    </jdbc:initialize-database>


</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:batch="http://www.springframework.org/schema/batch" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc">

<!--    <import resource="../config/spring-batch-contextOriginal.xml"/> 
 -->    
     <bean id="itemReader" class="org.springframework.batch.item.database.JdbcCursorItemReader"  scope="step">
        <property name="dataSource" ref="dataSource"/> 

         <property name="sql" value="SELECT internal_Id,individual_Id FROM Person" />

        <property name="rowMapper">
            <bean class="sb.dbToXml.PersonRowMapper"></bean>
        </property>

    </bean>


    <bean id="itemWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter">
       <property name="resource" value="file:xml/persons.xml"/>
       <property name="marshaller"  ref="personMarshaller"/>
       <property name="rootTagName" value="persons"/>
    </bean>

    <bean id="personMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
       <property name="classesToBeBound">
          <value>sb.dbToxml.Person </value>
       </property>
    </bean>



     <batch:job id="personJob">
        <batch:step id="step1" >
            <batch:tasklet transaction-manager="transactionManager">
                <batch:chunk reader="itemReader" writer="itemWriter"   commit-interval="10" />
            </batch:tasklet>
        </batch:step>

        <batch:listeners>
            <batch:listener ref="jobListener" />
        </batch:listeners> 
    </batch:job>


 </beans>
package sb.dbToxml;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionException;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainDbToXml
{

  public static void main(String[] args)
  {
    ApplicationContext context=new ClassPathXmlApplicationContext("Spring/batch/config/spring-batch-context.xml");

    JobLauncher jobLauncher= (JobLauncher) context.getBean("jobLauncher");

    Job job=(Job) context.getBean("personJob");

    try
    {
      JobExecution execution=jobLauncher.run(job, new JobParameters());
      System.out.println("Main/try :Job Person  Exit Status "+execution.getStatus());
    }
    catch (JobExecutionException e)
    {
      System.out.println("Main /catch :Job Person  failed");
      e.printStackTrace();
    }
  }

}

Person类:

package sb.dbToxml;

import javax.xml.bind.annotation.XmlAccessOrder;
import javax.xml.bind.annotation.XmlAccessorOrder;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="Person")
//@XmlAccessorOrder(XmlAccessOrder.UNDEFINED)
public class Person
{
  Long internal_id;
  Long Individual_id;

  @XmlElement(name="int_id")
  public Long getInternal_id()
  {
    return internal_id;
  }
  public void setInternal_id(Long internal_id)
  {
    this.internal_id = internal_id;
  }
  @XmlElement(name="indv_id")
  public Long getIndividual_id()
  {
    return Individual_id;
  }
  public void setIndividual_id(Long individual_id)
  {
    Individual_id = individual_id;
  }


}

PersonraWMapper:

package sb.dbToxml;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

public class PersonRowMapper implements RowMapper <Person>
{

  @Override
  public Person mapRow(ResultSet rs, int rowNum) throws SQLException
  {
    Person person=new Person();
    person.setIndividual_id(rs.getLong("individual_id"));
    person.setInternal_id(rs.getLong("internal_id"));
    return person;
  }

}

共有1个答案

海典
2023-03-14

MapJobRepositoryFactoryBean没有属性DataSourceTransactionManagerDatabaseType。您应该使用JobRepositoryFactoryBean而不是MapJobRepositoryFactoryBean:

因此请替换以下内容:

<!-- JobRepository and JobLauncher are configuration/setup classes -->
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean" >
   <property name="dataSource" ref="dataSource" /> 
   <property name="transactionManager" ref="transactionManager" />
   <property name="databaseType" value="oracle" /> 
</bean>

有了这个:

<!-- JobRepository and JobLauncher are configuration/setup classes -->
<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>
 类似资料:
  • context.xml 感谢Sh AntiBhushan

  • 有人知道spring batch framework在尝试将某些数据存储在batch_*表中时使用了多少个连接吗? 我们正试图估计连接池大小,我们知道我们的域数据库需要多少连接,但我们在diff db中有批处理模式,并且希望正确设置池大小,因为我们从池中得到错误,例如无法对象数据库连接

  • 本文向大家介绍Spring batch批处理框架,包括了Spring batch批处理框架的使用技巧和注意事项,需要的朋友参考一下 spring batch框架的简介 批处理任务是大多数IT项目的一个重要组成部分,批处理在业务系统中负责处理海量的数据,无须人工干预就能够自动高效的进行复杂的数据分析和处理。批处理会定期读入批量数据,经过相应的业务处理进行归档的业务操作,批处理的特征是自动执行,处理的

  • 我无法使用R Studio连接到Oracle数据库。 下面是我为设置连接而执行的代码。 以上代码已成功执行。 我在下面的代码中得到一个错误。 错误. jcall(drv@jdrv,"Ljava/sql/Connection;","Connec",as.character(url)[1],:java.sql.SQLExctive:指定无效的Oracle URL 我的R版本是3.4.0

  • 问题内容: 我们正在尝试借助Spring Framework实现Oracle连接池。我们正在使用DBCP连接池方法。但是,DBCP和spring之间的集成并不是很好。 我们面临的问题是,DBCP返回PoolableConnections对象,而Oracle期望使用OracleConnection对象。(引发ClassCastException) 看来此问题已在Oracle 11g中解决。但是,我对

  • 本文向大家介绍Spring Batch批处理框架使用解析,包括了Spring Batch批处理框架使用解析的使用技巧和注意事项,需要的朋友参考一下 这篇文章主要介绍了Spring Batch批处理框架使用解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 使用Spring Batch做为批处理框架,可以完成常规的数据量不是特别大的离线计算。 现在