Skip to content

@StepScope not working when XML namespace activated #3936

Closed
@farnetto

Description

@farnetto

I believe the problem described in the last comment in #1569 still exists: #1569 (comment)

If the Spring Batch XML namespace is loaded with CoreNamespaceUtils, and then you try to define a @StepScope bean in Java e.g. in a Configuration or Component class, then the bean is proxied twice and you get an exception during batch execution:

java.lang.ClassCastException: com.sun.proxy.$Proxy173 cannot be cast to org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader
	at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader$$FastClassBySpringCGLIB$$ebb633d0.invoke(<generated>) ~[spring-batch-infrastructure-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.2.13.RELEASE.jar:5.2.13.RELEASE]
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771) ~[spring-aop-5.2.13.RELEASE.jar:5.2.13.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.2.13.RELEASE.jar:5.2.13.RELEASE]
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) ~[spring-aop-5.2.13.RELEASE.jar:5.2.13.RELEASE]
	at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:136) ~[spring-aop-5.2.13.RELEASE.jar:5.2.13.RELEASE]
	at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:124) ~[spring-aop-5.2.13.RELEASE.jar:5.2.13.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.13.RELEASE.jar:5.2.13.RELEASE]
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) ~[spring-aop-5.2.13.RELEASE.jar:5.2.13.RELEASE]
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691) ~[spring-aop-5.2.13.RELEASE.jar:5.2.13.RELEASE]
	at org.springframework.batch.item.file.FlatFileItemReader$$EnhancerBySpringCGLIB$$9569ac9c.open(<generated>) ~[spring-batch-infrastructure-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	at org.springframework.batch.item.support.CompositeItemStream.open(CompositeItemStream.java:103) ~[spring-batch-infrastructure-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	at org.springframework.batch.core.step.tasklet.TaskletStep.open(TaskletStep.java:311) ~[spring-batch-core-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:205) ~[spring-batch-core-4.2.5.RELEASE.jar:4.2.5.RELEASE]

I have a bean called scopedTarget.scopedTarget.reader in my context, which is obviously wrong.

The XML namespace creates an instance of StepScope under the name "org.springframework.batch.core.scope.internalStepScope" with autoProxy=true and AbstractBatchConfiguration.ScopeConfiguration creates an instance with autoProxy=false.

There is a check in BatchScopeSupport#postProcessBeanFactory if the bean name starts with the target name prefix, but the value of StepScope.TARGET_NAME_PREFIX doesn't catch it: stepScopedTarget.

Should StepScope.TARGET_NAME_PREFIX be corrected? I don't see where else it is used.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions