做室内设计的网站有哪些内容,水果网站建设规划书,天津市建设工程交易网,如何撤销网站备案最近#xff0c;我协助一个客户启动并运行了Spring Batch实现。 该团队决定继续使用针对批处理作业的基于JavaConfig的配置#xff0c;而不是传统的基于XML的配置。 随着这越来越成为配置Java应用程序的一种常用方法#xff0c;我觉得是时候更新Keyhole的Spring Batch系列了… 最近我协助一个客户启动并运行了Spring Batch实现。 该团队决定继续使用针对批处理作业的基于JavaConfig的配置而不是传统的基于XML的配置。 随着这越来越成为配置Java应用程序的一种常用方法我觉得是时候更新Keyhole的Spring Batch系列了 向您展示如何将现有的基于XML的Spring Batch配置转换为基于JavaConfig批注的新配置。 本教程将使用在我们的第二批Spring教程 https://keyholesoftware.com/2012/06/25/getting-started-with-spring-batch-part-two/ 中找到的简单批处理作业。 房子清洁 在开始转换过程之前我们需要对项目进行一些房屋清洁。 尚未将Java构建和Spring环境升级到Java 7。 将Spring Boot依赖项添加到Maven pom.xml dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-batch/artifactIdversion1.2.4.RELEASE/version
/dependency 将Spring Batch版本修改为3.0.4.RELEASE并将Spring Framework版本修改为4.1.6.RELEASE properties spring.framework.version4.1.6.RELEASE/spring.framework.versionspring.batch.version3.0.4.RELEASE/spring.batch.version
/properties 在名为module-context.xml的原始批处理配置文件中注释掉作业定义。 在名为launch-context.xml的配置文件中注释掉Spring应用程序上下文配置元素。 注释掉ReaderProcessor和Writer元素上的Component批注。 不要注释掉CurrencyConversionServiceImpl类上的Service批注。 构建基于JavaConfig的配置 现在我们已经删除或禁用了现有的基于XML的配置我们可以开始构建基于JavaConfig的配置。 为此我们需要创建一个带有一些注释的新类这些注释为配置奠定了基础。 package com.keyhole.example.config;import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;Configuration
EnableBatchProcessing
public class TickerPriceConversionConfig {Autowiredprivate JobBuilderFactory jobs;Autowiredprivate StepBuilderFactory steps;} Configuration注释使Spring容器知道该类将包含一个或多个Bean注释的方法这些方法将在运行时进行处理以生成Bean定义和服务请求。 EnableBatchProcessing批注提供了用于构建批处理作业配置的基本配置。 Spring Batch使用此注释来设置默认的JobRepositoryJobLauncherJobRegistryPlatformTransactionManagerJobBuilderFactory和StepBuilderFactory。 现在是时候为组成批处理作业的组件添加Bean注释的方法了。 作为参考我为每个bean提供了相应的XML配置。 ItemReader配置 bean nametickerReaderclassorg.springframework.batch.item.file.FlatFileItemReader
property nameresource
valuehttp://finance.yahoo.com/d/quotes.csv?sXOMIBMJNJMSFTfsnd1ol1p2 /property namelineMapper reftickerLineMapper /
/beanbean nametickerLineMapper
classorg.springframework.batch.item.file.mapping.DefaultLineMapper
property namefieldSetMapper reftickerMapper /property namelineTokenizer reftickerLineTokenizer /
/beanbean nametickerLineTokenizer
classorg.springframework.batch.item.file.transform.DelimitedLineTokenizer /Beanpublic ItemReaderTickerData reader() throws MalformedURLException {FlatFileItemReaderTickerData reader new FlatFileItemReaderTickerData();reader.setResource(new UrlResource(http://finance.yahoo.com/d/quotes.csv?sXOMIBMJNJMSFTfsnd1ol1p2));reader.setLineMapper(new DefaultLineMapperTickerData() {{setLineTokenizer(new DelimitedLineTokenizer());setFieldSetMapper(new TickerFieldSetMapper());}});return reader;} ItemProcessor和ItemWriter以前使用Spring容器的Component注释来拾取bean并将其加载到应用程序上下文中。 Beanpublic ItemProcessorTickerData, TickerData processor() {return new TickerPriceProcessor();}Beanpublic ItemWriterTickerData writer() {return new LogItemWriter();} 现在我们已经定义了Spring bean我们可以创建Bean注释的方法来表示步骤和工作。 作为参考我包括了相应的XML配置。 batch:job idTickerPriceConversionbatch:step idconvertPricebatch:tasklet transaction-managertransactionManagerbatch:chunk readertickerReaderprocessortickerPriceProcessorwritertickerWriter commit-interval10 //batch:tasklet/batch:step/batch:jobBeanpublic Job TickerPriceConversion() throws MalformedURLException {return jobs.get(TickerPriceConversion).start(convertPrice()).build();}Beanpublic Step convertPrice() throws MalformedURLException {return steps.get(convertPrice).TickerData, TickerData chunk(5).reader(reader()).processor(processor()).writer(writer()).build();} 我将在文章的末尾包含TickerPriceConversionConfig类的完整代码以供参考但基本上这就是全部 一旦定义了Spring bean并使用JobBuilderFactory和StepBuilderFactory为批处理作业和步骤创建Bean配置就可以运行该作业并测试配置了。 为了运行作业我们将使用Spring Boot来测试新转换的作业配置的执行情况。 为此我们将在测试包中创建一个名为TickerPriceConversionJobRunner的新类。 源代码如下所示 package com.keyhole.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;SpringBootApplication
public class TickerPriceConversionJobRunner {public static void main(String[] args) {SpringApplication.run(TickerPriceConversionJobRunner.class, args);}} SpringBootApplication注释本质上是一个便捷注释它提供了通常使用 Configuration EnableAutoConfiguration和ComponentScan可以获得的功能。 TickerPriceConversionJobRunner是一个简单的Java应用程序它将主要方法处理委托给Spring Boot的SpringApplication类来运行该应用程序。 现在您可以将该项目导出为jar并从命令行运行TickerPriceConversionJobRunner或者如果您想在Spring STS中运行它则可以右键单击该类然后选择Run As→Spring Boot Application。 最终想法和代码清单 如您所见创建Spring Batch作业配置并不需要很多工作但是如果您决定将所有现有作业从基于XML的配置转换为较新的基于JavaConfig的配置摆在您前面的工作。 大部分工作将花费大量时间来充分回归测试转换后的批处理作业。 如果您拥有大量的Spring Batch作业库那将值得吗 可能不是但是如果您刚开始使用或具有可管理的批处理库那么这绝对是我会采用的方法。 TickerPriceConversionConfig的代码清单 package com.keyhole.example.config;import java.net.MalformedURLException;import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.UrlResource;import com.keyhole.example.LogItemWriter;
import com.keyhole.example.TickerData;
import com.keyhole.example.TickerFieldSetMapper;
import com.keyhole.example.TickerPriceProcessor;Configuration
EnableBatchProcessing
public class TickerPriceConversionConfig {Autowiredprivate JobBuilderFactory jobs;Autowiredprivate StepBuilderFactory steps;Beanpublic ItemReaderTickerData reader() throws MalformedURLException {FlatFileItemReaderTickerData reader new FlatFileItemReaderTickerData();reader.setResource(new UrlResource(http://finance.yahoo.com/d/quotes.csv?sXOMIBMJNJMSFTfsnd1ol1p2));reader.setLineMapper(new DefaultLineMapperTickerData() {{setLineTokenizer(new DelimitedLineTokenizer());setFieldSetMapper(new TickerFieldSetMapper());}});return reader;}Beanpublic ItemProcessorTickerData, TickerData processor() {return new TickerPriceProcessor();}Beanpublic ItemWriterTickerData writer() {return new LogItemWriter();}Beanpublic Job TickerPriceConversion() throws MalformedURLException {return jobs.get(TickerPriceConversion).start(convertPrice()).build();}Beanpublic Step convertPrice() throws MalformedURLException {return steps.get(convertPrice).TickerData, TickerData chunk(5).reader(reader()).processor(processor()).writer(writer()).build();}
} TickerPriceConversionJobRunner的代码清单 代码项目 package com.keyhole.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;SpringBootApplication
public class TickerPriceConversionJobRunner {public static void main(String[] args) {SpringApplication.run(TickerPriceConversionJobRunner.class, args);}}翻译自: https://www.javacodegeeks.com/2015/07/spring-batch-replacing-xml-job-configuration-with-javaconfig.html