- Spring Batch 教程
- Spring Batch - 首頁
- Spring Batch - 概述
- Spring Batch - 環境
- Spring Batch - 架構
- Spring Batch - 應用
- Spring Batch - 配置
- 讀取器、寫入器和處理器
- Spring Batch - 基本應用
- Spring Batch - XML 到 MySQL
- Spring Batch - CSV 到 XML
- Spring Batch - MySQL 到 XML
- Spring Batch - MySQL 到平面檔案
- Spring Batch 有用資源
- Spring Batch - 快速指南
- Spring Batch - 有用資源
- Spring Batch - 討論
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 的作業。它包含一組步驟,並且可以使用 JobLauncher 啟動。
此標籤具有如下所示的 2 個屬性:
| 序號 | 屬性 & 描述 |
|---|---|
| 1 | Id 它是作業的 Id,必須為此屬性指定值。 |
| 2 | restartable 此屬性用於指定作業是否可重啟。此屬性是可選的。 |
以下是 SpringBatch 作業的 XML 配置。
<job id = "jobid" restartable = "false" > . . . . . . . . . . . . . . . . . . . . . . . . // Step definitions </job>
步驟 (Step)
此標籤用於定義/配置 SpringBatch 作業的步驟。它具有以下三個屬性:
| 序號 | 屬性 & 描述 |
|---|---|
| 1 | Id 它是作業的 Id,必須為此屬性指定值。 |
| 2 | next 它是指定下一步的快捷方式。 |
| 3 | parent 它用於指定配置應從中繼承的父 bean 的名稱。 |
以下是 SpringBatch 步驟的 XML 配置。
<job id = "jobid"> <step id = "step1" next = "step2"/> <step id = "step2" next = "step3"/> <step id = "step3"/> </job>
塊 (Chunk)
此標籤用於定義/配置任務塊的塊。它具有以下四個屬性:
| 序號 | 屬性 & 描述 |
|---|---|
| 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 它用於指定在提交事務之前要處理的專案數量。 |
以下是 SpringBatch 塊的 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)
TransactionManager bean 用於使用關係資料庫配置 TransactionManager。此 bean 與型別為org.springframework.transaction.platform.TransactionManager的類關聯。
<bean id = "transactionManager" class = "org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
資料來源 (DataSource)
資料來源 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.cj.jdbc.Driver" /> <property name = "url" value = "jdbc:mysql://:3306/details" /> <property name = "username" value = "myuser" /> <property name = "password" value = "password" /> </bean>