【疑难杂症】springboot新版本中使用springbatch的stepscope功能-引出bean注册报错

【疑难杂症】springboot新版本中使用springbatch的stepscope功能-引出bean注册报错

内容

maven版本信息

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.3</version>
    </parent>

    对应springbatch版本
    spring-batch-core-4.3.4

job配置示例xml:

    <bean id="t1000Tasklet" class="com.company.project.batch.handleTAccountInfoJob.T1000Tasklet" scope="step"/>

报错日志:这里已分行显示,便于定位

Caused by: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 't1000Tasklet' defined in BeanDefinition defined in class path resource [batch/job/user2job/steps/Step1.xml]: 

Cannot register bean definition [Root bean: class [org.springframework.aop.scope.ScopedProxyFactoryBean]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; 

defined in BeanDefinition defined in class path resource [batch/job/user2job/steps/Step1.xml]] for bean 't1000Tasklet': 

There is already [Generic bean: class [com.company.project.batch.handleTAccountInfoJob.T1000Tasklet]; scope=step; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=false; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [batch/job/user2job/steps/Step1.xml]] bound.
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.registerBeanDefinition(DefaultListableBeanFactory.java:1004)
	at org.springframework.batch.core.scope.BatchScopeSupport.createScopedProxy(BatchScopeSupport.java:159)
	at org.springframework.batch.core.scope.BatchScopeSupport.postProcessBeanFactory(BatchScopeSupport.java:130)

如何解决:

  1. 方案1 退回到springbatch 3.xx版本
  2. 方案2 应用增加启动参数 允许重复覆盖bean

这里说下方案2的参数

spring.main.allow-bean-definition-overriding=true

究其原因,spring默认容器已经注册过bean,但使用stepscope模式时,会将stepbean的代理类注册到StepScope容器中。

如果springbatch能兼容的更好,这种类型本不该在启动时就注册。最好在启动job时才生成step级别实例。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值