Spring Batch配置

在編寫Spring Batch應用程式時,我們將使用Spring Batch命名空間中提供的XML標記來配置作業,步驟,JobLauncher,JobRepository,事務管理器,讀取器和寫入器。 因此,您需要將此名稱空間包含在XML檔中,如下所示。

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

   http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
   http://www.springframework.org/schema/bean
   http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

在下面的章節中,我們將討論Spring Batch命名空間中可用的各種標籤,它們的屬性和示例。

作業

<job>標籤用於定義/配置SpringBatch的作業。 它包含一系列步驟(step),可以使用JobLauncher啟動。

<job>標籤有2個屬性,如下所列 -

編號 屬性 描述
1 id 它是作業的標識,必須指定該屬性的值。
2 restartable 這是用於指定作業是否可重新啟動的屬性。 該屬性是可選的。

以下是Spring Batch作業的XML配置。

<job id = "jobid" restartable = "false" >
   . . . . . . . .
   . . . . . . . .
   . . . . . . . . // Step definitions
</job>

步驟

<step>標籤用於定義/配置Spring Batch作業的步驟。 它有以下三個屬性 -

編號 屬性 描述
1 id 它是作業的標識,必須指定該屬性的值。
2 next 這是指定下一步的快捷方式。
3 parent 它用於指定配置應從其繼承的父bean的名稱。

以下是Spring Batch步驟的XML配置。

<job id = "jobid">
   <step id = "step1" next = "step2"/>
   <step id = "step2" next = "step3"/>
   <step id = "step3"/>
</job>

Chunk

這個標籤用於定義/配置一個tasklet塊。它有以下四個屬性 -

編號 屬性 描述
1 reader 它表示專案讀取器bean的名稱。 它接受org.springframework.batch.item.ItemReader類型的值。
2 writer 它代表專案寫入器bean的名稱。 它接受org.springframework.batch.item.ItemWriter類型的值。
3 processor 它代表專案讀取器bean的名稱。 它接受類型org.springframework.batch.item.ItemProcessor的值。
4 commit-interval

以下是Spring Batch塊的XML配置。

<batch:step id = "step1">
   <batch:tasklet>
      <batch:chunk reader = "xmlItemReader"
         writer = "mysqlItemWriter" processor = "itemProcessor" commit-interval = "10">
      </batch:chunk>
   </batch:tasklet>
</batch:step>

JobRepository

JobRepository Bean用於使用關係資料庫來配置JobRepository。 這個bean與org.springframework.batch.core.repository.JobRepository類型的類相關聯。

編號 屬性 描述
1 dataSource 它用於指定定義數據源的bean名稱。
2 transactionManager 它用於指定定義事務管理器的bean的名稱。
3 databaseType 它指定作業存儲庫中使用的關係資料庫的類型。

以下是JobRepository的示例配置。

<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 = "mysql" />
</bean>

JobLauncher

JobLauncher bean用於配置JobLauncher。 它與類org.springframework.batch.core.launch.support.SimpleJobLauncher(在我們的程式中)相關聯。 這個bean有一個名為jobrepository的屬性,它用來指定定義jobrepository的bean的名字。

以下是jobLauncher的示例配置。

<bean id = "jobLauncher"
   class = "org.springframework.batch.core.launch.support.SimpleJobLauncher">
   <property name = "jobRepository" ref = "jobRepository" />
</bean>

事務管理
TransactionManager bean用於使用關係資料庫來配置TransactionManager。 這個bean與類型為org.springframework.transaction.platform.TransactionManager的類相關聯。

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

數據源

數據源bean用於配置數據源。 這個bean與類型為org.springframework.jdbc.datasource.DriverManagerDataSource的類相關聯。

編號 屬性 描述
1 driverClassName 這指定用於連接資料庫的驅動程式的類名稱。
2 url 這指定了資料庫的URL。
3 username 這指定了連接資料庫的用戶名。
4 password 這指定了與資料庫連接的密碼。

以下是數據源的示例配置。

<bean id = "dataSource"
   class = "org.springframework.jdbc.datasource.DriverManagerDataSource">
   <property name = "driverClassName" value = "com.mysql.jdbc.Driver" />
   <property name = "url" value = "jdbc:mysql://localhost:3306/details" />
   <property name = "username" value = "myuser" />
   <property name = "password" value = "password" />
</bean>

上一篇: Spring Batch應用程式 下一篇: Spring Batch讀取器,寫入器和處理器