使用spring batch和spring boot 2.2.5时,无法从xml配置注册自定义LineMapper
版本使用:使用spring batch和spring boot 2.2.5时,无法从xml配置注册自定义LineMapper,spring,spring-boot,spring-batch,Spring,Spring Boot,Spring Batch,版本使用: 弹簧靴2.2.5 弹簧批核心4.x spring-batch.xml文件中只有以下一个bean: <bean id="lineMapper" primary="true" class="com.batch.ContextSavingLineMapper" scope="step"> <property name="lineTokenizer"> <bean class="org.springframework.
- 弹簧靴2.2.5
- 弹簧批核心4.x
<bean id="lineMapper" primary="true" class="com.batch.ContextSavingLineMapper" scope="step">
<property name="lineTokenizer">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<!-- This is to accept lines with incorrect number of tokens -->
<property name="strict" value="false"/>
<!-- Expected CSV column names -->
<property name="names" value="#{jobParameters['columnNames']}" />
</bean>
</property>
<property name="fieldSetMapper">
<bean class="com.batch.DetectorRegistrationFieldSetMapper" />
</property>
</bean>
注意:我不想使用
spring.main.allow bean definition overriding=true
您遇到了这个公开问题:
我不想使用spring.main.allow bean definition overriding=true
不幸的是,在上述问题得到解决之前,这是唯一的解决办法。请提供一个复制该问题的解决方案,我会看一看。我正在做的项目非常大,但我可以给你一个类似的解决方案,这是github的项目,我认为他们遇到了相同的问题:想象一下,您只有1个bean定义,如上所述,当您使用spring boot运行应用程序时,您会收到错误消息,您有2个同名的bean(据我所知)。真奇怪。我使用的是spring batch core 4.x、spring boot 2.2.5,spring batch是通过XML配置的,而不是使用spring boot中的BatchAutoConfiguration。
lineMapper
是否可以重命名该ID?当两个bean的名称相同时会发生错误,因为上下文中不存在>1。是的,我尝试将lineMapper重命名为lineMapper1进行测试。我已经收到消息:无法注册在类路径资源[spring batch.xml]中定义的BeanDefinition中定义的bean'lineMapper1'。具有该名称的bean已经在类路径资源[spring batch.xml]中定义,并且重写被禁用代码>相反。哦,明白了。如果我将所有XML配置移到Springboot Java配置,是否有可能解决这个问题?实际上,社区中有一些原因表明我们不在项目中使用spring.main.allow bean definition overriding=true,例如。如果项目很大,我们有一个5到10人的团队。当然,通过Spring引导和@EnableBatchProcessing
将XML配置移动到Java配置是另一种方法。我没有这样建议,因为我不知道你的限制是什么。但如果这是你的选择,那么我会推荐它。
public class ContextSavingLineMapper<T> extends DefaultLineMapper<T> implements StepExecutionListener {
Description:
The bean 'lineMapper', defined in BeanDefinition defined in class path resource [spring-batch.xml], could not be registered. A bean with that name has already been defined in class path resource [spring-batch.xml] and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true