目录

Spring Batch - Configuration

在编写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个属性,如下所示 -

S.No 属性和描述
1

Id

它是作业的ID,必须为此属性指定值。

2

restartable

这是用于指定作业是否可重新启动的属性。 此属性是可选的。

以下是SpringBatch作业的XML配置。

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

Step

此标记用于定义/配置SpringBatch作业的步骤。 它具有以下三个属性 -

S.No 属性和描述
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

此标记用于定义/配置tasklet一个块。 它具有以下四个属性 -

S.No 属性和描述
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类型的类相关联。

S.No 属性和描述
1

dataSource

它用于指定定义数据源的bean名称。

2

transactionManager

它用于指定定义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用于配置Datasource 。 此bean与org.springframework.jdbc.datasource.DriverManagerDataSource类型的类相关联。

S.No 属性和描述
1

driverClassName

它指定用于连接数据库的驱动程序的类名。

2

url

这指定了数据库的URL。

3

username

这指定了与数据库连接的用户名。

4

password

这指定了与数据库连接的密码。

以下是datasource的示例配置。

<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> 
↑回到顶部↑
WIKI教程 @2018