Spirng_Batch
一、需求分析
使用Spring Batch对XML文件进行读写操作: 从一个xml文件中读取商品信息, 经过简单的处理, 写入另外一个xml文件中.
二、代码实现
1. 代码结构图:

2. applicationContext.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" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd"
default-autowire="byName"> <!-- auto scan path -->
<context:component-scan base-package="com.zdp" /> <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean" />
<bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
</beans>
base-package: 扫描spring注解
jobLauncher: 启动Job
jobRepository: 为Job提供持久化操作
transactionManager: 提供事务管理操作
3. springBatch.xml
<?xml version="1.0" encoding="UTF-8"?>
<bean:beans xmlns="http://www.springframework.org/schema/batch"
xmlns:bean="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd"> <bean:import resource="applicationContext.xml" /> <job id="xmlFileReadAndWriterJob">
<step id="xmlFileReadAndWriterStep">
<tasklet>
<chunk reader="xmlReader" writer="xmlWriter" processor="xmlProcessor" commit-interval="10" />
</tasklet>
</step>
</job> <!-- XML文件读取 -->
<bean:bean id="xmlReader" class="org.springframework.batch.item.xml.StaxEventItemReader" scope="step">
<bean:property name="fragmentRootElementName" value="product" />
<bean:property name="unmarshaller" ref="tradeMarshaller" />
<bean:property name="resource" value="file:#{jobParameters['inputFilePath']}" />
</bean:bean> <!-- XML文件写入 -->
<bean:bean id="xmlWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter" scope="step">
<bean:property name="rootTagName" value="zdp" />
<bean:property name="marshaller" ref="tradeMarshaller" />
<bean:property name="resource" value="file:#{jobParameters['outputFilePath']}" />
</bean:bean> <bean:bean id="tradeMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
<bean:property name="aliases">
<util:map id="aliases">
<bean:entry key="product" value="com.zdp.domain.Product" />
<bean:entry key="buyDate" value="java.util.Date" />
</util:map>
</bean:property>
</bean:bean>
</bean:beans>
1. Job包含一个Step,Step中包含了基本的读(xmlReader),处理(xmlProcessor),写(xmlWriter)。
2. xmlReader配置了xml读操作, resource属性是指定文件路径信息的。知道了文件路径 。 fragmentRootElementName属性是指定根节点名称的. unmarshaller负责完成解析节点信息,并映射成程序pojo对象。
3. tradeMarshaller为解析xml节点, 其中entry的key指定对应根节点名称product,value指定程序的pojo类,这样,程序就可以将product节点下的子节点与pojo类(P roduct )中的属性去匹配,当匹配到子节点名与pojo类中的属性名相同时,就会将子节点的内容赋值给pojo类的属性。这样就完成了一个根节点的读取,框架会控制循环操作,直到将文件中所有根( product )节点全部读完为止。这样就完成了XML文件的读操作。
4. xmlWriter配置了对XML文件的写操作。resource属性提供文件的路径信息。同时,也是需要知道这个文件的跟节点信息的,rootTagName属性提供根节点名信息。marshaller 把pojo对象转换成XML片段的工具。本文读操作的unmarshaller和写操作的marshaller用的是同一个转换器,因为XStreamMarshaller既提供将节点片段转换为pojo对象功能,同时又提供将pojo对象持久化为xml文件的功能。
4. XMLProcessor
/**
* XML文件处理类。
*/
@Component("xmlProcessor")
public class XMLProcessor implements ItemProcessor<Product, Product> {
// XML文件内容处理
@Override
public Product process(Product product) throws Exception {
product.setBuyDate(new Date());
product.setCustomer(product.getCustomer() + "顾客!");
product.setId(product.getId() + "_1");
product.setPrice(product.getPrice() + 1000.0);
product.setQuantity(product.getQuantity() + 100);
return product;
}
}
5. Product
/**
* 实体产品类
*/
public class Product {
private String id;
private int quantity; // 数量
private double price; // 价格
private String customer; // 顾客
private Date buyDate; // 日期
}
6. JobLaunch
/**
* Test client
*/
public class JobLaunch { public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("springBatch.xml");
JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
Job job = (Job) context.getBean("xmlFileReadAndWriterJob");
try {
jobLauncher.run(job, new JobParametersBuilder().addString("inputFilePath", "d:\\input.xml")
.addString("outputFilePath", "d:\\output.xml")
.toJobParameters());
} catch (Exception e) {
e.printStackTrace();
}
}
}
7. input.xml

8. output.xml

http://www.tuicool.com/articles/NNZbYv3
http://www.dineshonjava.com/2014/03/spring-batch-xml-to-mongodb-database.html http://stackoverflow.com/questions/26462952/how-to-extract-data-from-multiple-xml-files-and-put-it-in-one-xml-file-in-spring?rq=1 http://www.dineshonjava.com/2014/03/spring-batch-xml-to-mongodb-database.html
mutiple: http://www.dineshonjava.com/2014/03/multiresourceitemreader-in-spring-batch.html
override reader http://stackoverflow.com/questions/17448541/multiresourceitemreader-with-a-custom-delegate-keeps-reading-the-same-file
Spirng_Batch的更多相关文章
随机推荐
- js控制不同的时间段显示不同的css样式
js控制不同的时间段显示不同的css样式 js函数,可以放到单独的js文件中也可以放到当前页的<head>标记之内 function getCSS(){ datetoday ...
- Javascript——说说js的调试
最近比较吐槽,大家都知道,现在web前端相对几年前来说已经变得很重了,各种js框架,各种面对对象,而且项目多了,就会提取公共模块. 这些模块的UI展示都一样,不一样的就是后台逻辑,举个例子吧,我们做企 ...
- 安卓升级提示 phoneGap APK软件更新提示
以下代码由PHP200 阿杜整理 package com.example.syzx; import java.io.BufferedReader; import java.io.File; imp ...
- JavaFx2.0中CSS的应用
http://user.qzone.qq.com/773534839#!app=2&via=QZ.HashRefresh&pos=1326994508 ———————————————— ...
- 用一个例子学习CSS的伪类元素
CSS伪类元素是一个非常酷的东西!首先我们理解一下它,:before :after 伪类元素,也就是虚假的元素.它可以插入在元素的前面或者后面,而在HTML文档结构中,它却是不存在的,因为Js是无法通 ...
- [iOS微博项目 - 1.3] - 内容对齐 TextAlignment & VerticalAlignment & HorizontalAlignment & contentMode
四个容易混淆的属性:1. textAligment : 文字的水平方向的对齐方式1> 取值NSTextAlignmentLeft = 0, // 左对齐NSTextAlignme ...
- AngularJS 学习笔记二
AngularJS指令 指令 描述 讲解 ng_app 定义应用程序的根元素. 指令 ng_bind 绑定 HTML 元素到应用程序数据. 简介 ng_click 定义元素被单击时的行为. HTML ...
- C#中的ref和out的区别
转载原地址 http://www.cnblogs.com/gjahead/archive/2008/02/28/1084871.html ref和out的区别在C# 中,既可以通过值也可以通过引用传递 ...
- LCA算法
LCA算法: LCA(Least Common Ancestor),顾名思义,是指在一棵树中,距离两个点最近的两者的公共节点.也就是说,在两个点通往根的道路上,肯定会有公共的节点,我们就是要求找到公共 ...
- viPlugin安装破解
viPlugin是一个eclipse 针对vi的插件,使用此插件可以让你在使用eclipse进行编码时使用几乎所有vi命令,可以极大的提高开发编码效率. 安装 点击你的eclipse中的 help ...