日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

Spring Batch –用JavaConfig替换XML作业配置

發(fā)布時間:2023/12/3 asp.net 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Batch –用JavaConfig替换XML作业配置 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

最近,我協(xié)助一個客戶啟動并運行了Spring Batch實現(xiàn)。 該團隊決定繼續(xù)使用針對批處理作業(yè)的基于JavaConfig的配置,而不是傳統(tǒng)的基于XML的配置。 隨著這越來越成為配置Java應用程序的一種常用方法,我覺得是時候更新Keyhole的Spring Batch系列了 ,向您展示如何將現(xiàn)有的基于XML的Spring Batch配置轉換為基于JavaConfig批注的新配置。

本教程將使用在我們的第二批Spring教程( https://keyholesoftware.com/2012/06/25/getting-started-with-spring-batch-part-two/ )中找到的簡單批處理作業(yè)。

房子清潔

在開始轉換過程之前,我們需要對項目進行一些房屋清潔。

  • 尚未將Java構建和Spring環(huán)境升級到Java 7。
  • 將Spring Boot依賴項添加到Maven pom.xml:
  • <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-batch</artifactId><version>1.2.4.RELEASE</version> </dependency>
  • 將Spring Batch版本修改為3.0.4.RELEASE,并將Spring Framework版本修改為4.1.6.RELEASE <properties> <spring.framework.version>4.1.6.RELEASE</spring.framework.version><spring.batch.version>3.0.4.RELEASE</spring.batch.version> </properties>
  • 在名為module-context.xml的原始批處理配置文件中注釋掉作業(yè)定義。
  • 在名為launch-context.xml的配置文件中注釋掉Spring應用程序上下文配置元素。
  • 注釋掉Reader,Processor和Writer元素上的@Component批注。 不要注釋掉CurrencyConversionServiceImpl類上的@Service批注。
  • 構建基于JavaConfig的配置

    現(xiàn)在我們已經(jīng)刪除或禁用了現(xiàn)有的基于XML的配置,我們可以開始構建基于JavaConfig的配置。 為此,我們需要創(chuàng)建一個帶有一些注釋的新類,這些注釋為配置奠定了基礎。

    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批注提供了用于構建批處理作業(yè)配置的基本配置。 Spring Batch使用此注釋來設置默認的JobRepository,JobLauncher,JobRegistry,PlatformTransactionManager,JobBuilderFactory和StepBuilderFactory。

    現(xiàn)在是時候為組成批處理作業(yè)的組件添加@Bean注釋的方法了。 作為參考,我為每個bean提供了相應的XML配置。

    ItemReader配置

    <bean name="tickerReader"class="org.springframework.batch.item.file.FlatFileItemReader"> <property name="resource" value="http://finance.yahoo.com/d/quotes.csv?s=XOM+IBM+JNJ+MSFT&f=snd1ol1p2" /><property name="lineMapper" ref="tickerLineMapper" /> </bean><bean name="tickerLineMapper" class="org.springframework.batch.item.file.mapping.DefaultLineMapper"> <property name="fieldSetMapper" ref="tickerMapper" /><property name="lineTokenizer" ref="tickerLineTokenizer" /> </bean><bean name="tickerLineTokenizer" class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer" />@Beanpublic ItemReader<TickerData> reader() throws MalformedURLException {FlatFileItemReader<TickerData> reader = new FlatFileItemReader<TickerData>();reader.setResource(new UrlResource("http://finance.yahoo.com/d/quotes.csv?s=XOM+IBM+JNJ+MSFT&f=snd1ol1p2"));reader.setLineMapper(new DefaultLineMapper<TickerData>() {{setLineTokenizer(new DelimitedLineTokenizer());setFieldSetMapper(new TickerFieldSetMapper());}});return reader;}

    ItemProcessor和ItemWriter以前使用Spring容器的@Component注釋來拾取bean并將其加載到應用程序上下文中。

    @Beanpublic ItemProcessor<TickerData, TickerData> processor() {return new TickerPriceProcessor();}@Beanpublic ItemWriter<TickerData> writer() {return new LogItemWriter();}

    現(xiàn)在我們已經(jīng)定義了Spring bean,我們可以創(chuàng)建@Bean注釋的方法來表示步驟和工作。 作為參考,我包括了相應的XML配置。

    <batch:job id="TickerPriceConversion"><batch:step id="convertPrice"><batch:tasklet transaction-manager="transactionManager"><batch:chunk reader="tickerReader"processor="tickerPriceProcessor"writer="tickerWriter" commit-interval="10" /></batch:tasklet></batch:step></batch:job>@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();}

    我將在文章的末尾包含TickerPriceConversionConfig類的完整代碼,以供參考,但基本上這就是全部!

    一旦定義了Spring bean并使用JobBuilderFactory和StepBuilderFactory為批處理作業(yè)和步驟創(chuàng)建Bean配置,就可以運行該作業(yè)并測試配置了。 為了運行作業(yè),我們將使用Spring Boot來測試新轉換的作業(yè)配置的執(zhí)行情況。 為此,我們將在測試包中創(chuàng)建一個名為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注釋本質(zhì)上是一個便捷注釋,它提供了通常使用@ Configuration,@ EnableAutoConfiguration和@ComponentScan可以獲得的功能。 TickerPriceConversionJobRunner是一個簡單的Java應用程序,它將主要方法處理委托給Spring Boot的SpringApplication類來運行該應用程序。

    現(xiàn)在,您可以將該項目導出為jar并從命令行運行TickerPriceConversionJobRunner,或者,如果您想在Spring STS中運行它,則可以右鍵單擊該類,然后選擇Run As→Spring Boot Application。

    最終想法和代碼清單

    如您所見,創(chuàng)建Spring Batch作業(yè)配置并不需要很多工作,但是如果您決定將所有現(xiàn)有作業(yè)從基于XML的配置轉換為較新的基于JavaConfig的配置,擺在您前面的工作。 大部分工作將花費大量時間來充分回歸測試轉換后的批處理作業(yè)。

    如果您擁有大量的Spring Batch作業(yè)庫,那將值得嗎? 可能不是,但是如果您剛開始使用或具有可管理的批處理庫,那么這絕對是我會采用的方法。

    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 ItemReader<TickerData> reader() throws MalformedURLException {FlatFileItemReader<TickerData> reader = new FlatFileItemReader<TickerData>();reader.setResource(new UrlResource("http://finance.yahoo.com/d/quotes.csv?s=XOM+IBM+JNJ+MSFT&f=snd1ol1p2"));reader.setLineMapper(new DefaultLineMapper<TickerData>() {{setLineTokenizer(new DelimitedLineTokenizer());setFieldSetMapper(new TickerFieldSetMapper());}});return reader;}@Beanpublic ItemProcessor<TickerData, TickerData> processor() {return new TickerPriceProcessor();}@Beanpublic ItemWriter<TickerData> 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

    總結

    以上是生活随笔為你收集整理的Spring Batch –用JavaConfig替换XML作业配置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。