gpt4 book ai didi

java - 从 Spring 作业配置中删除重复代码

转载 作者:行者123 更新时间:2023-12-01 11:28:41 24 4
gpt4 key购买 nike

下面是我的步骤配置 -

<beans:bean id="myInputFileReader" class="com.rbos.fm.risk.batch.spring.reader.InputFileReader" scope="step">
<beans:property name="delegate">
<beans:bean class="org.springframework.batch.item.file.FlatFileItemReader"
scope="step">
<beans:property name="resource" ref="inputFileSystemResource" />
<beans:property name="linesToSkip" value="1" />
<beans:property name="lineMapper">
<beans:bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<beans:property name="lineTokenizer">
<beans:bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<beans:property name="delimiter" value="|"/>
</beans:bean>
</beans:property>
<beans:property name="fieldSetMapper">
<beans:bean class="com.test.MyFieldMapper1" scope="prototype"/>
</beans:property>
</beans:bean>
</beans:property>
</beans:bean>
</beans:property>
<beans:property name="param1" value="#{jobParameters['param1']}"/>
<beans:property name="param2" value="#{jobParameters['param2']}"/>
<beans:property name="param3" value="#{jobParameters['param3']}"/>
</beans:bean>


<beans:bean id="fileItemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
<beans:property name="resource" ref="outputFileSystemResource" />
<beans:property name="lineAggregator">
<beans:bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
<beans:property name="delimiter" value="|"/>
<beans:property name="fieldExtractor">
<beans:bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<beans:property name="names"
value="outcolomn1, outcolomn2, outcolomn3, outcolomn4"/>
</beans:bean>
</beans:property>
</beans:bean>
</beans:property>
</beans:bean>

<step id="myPreProcessing1">
<tasklet>
<chunk reader="myInputFileReader" processor="myFileProcessor1" writer="fileItemWriter"
commit-interval="10000"/>
</tasklet>
</step>

我必须添加其他(类似)步骤。我想使用相同的读取器和写入器,因为读取器中的唯一更改将是一个新的 FieldSetMapper ,而写入器将是一个新的 BeanWrapperFieldExtractor 。因此,理想情况下,我想在父级别指定读取器和写入器,并希望通过覆盖新的映射器和提取器来在所有步骤中引用它们。

最佳答案

使用Bean Definition Inheritance 。创建一个抽象 bean 定义,并让您的实际定义使用它作为父级

<beans:bean id="parentInputFileReader" class="com.rbos.fm.risk.batch.spring.reader.InputFileReader" scope="step" abstract="true">
<beans:property name="delegate">
<beans:bean class="org.springframework.batch.item.file.FlatFileItemReader">
<beans:property name="resource" ref="inputFileSystemResource" />
<beans:property name="linesToSkip" value="1" />
</beans:bean>
</beans:property>
<beans:property name="param1" value="#{jobParameters['param1']}"/>
<beans:property name="param2" value="#{jobParameters['param2']}"/>
<beans:property name="param3" value="#{jobParameters['param3']}"/>
</beans:bean>

<beans:bean id="parentLineMapper" class="org.springframework.batch.item.file.mapping.DefaultLineMapper" abstract="true">
<beans:property name="lineTokenizer">
<beans:bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<beans:property name="delimiter" value="|"/>
</beans:bean>
</beans:property>
</beans:bean>

现在您已经有了父 Bean,您可以使用 parent 属性引用它们,并且仅配置那些需要或需要修改默认值的属性。

<bean id="myInputFileReader" parent="parent">
<beans:property name="lineMapper">
<bean parent="parentLineMapper" >
<beans:property name="fieldSetMapper">
<beans:bean class="com.test.MyFieldMapper1" />
</beans:property>
</bean>
</beans:property />
</bean>

关于java - 从 Spring 作业配置中删除重复代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30616277/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com