Skip to content

@StepScope should default to ScopedProxyMode.TARGET_CLASS [BATCH-2023] #1569

Closed
@spring-projects-issues

Description

@spring-projects-issues

Tobias Flohre opened BATCH-2023 and commented

If I use @StepScope on a @Bean method that returns a FlatFileItemReader, I get a ClassCastException: java.lang.ClassCastException: $Proxy16 cannot be cast to org.springframework.batch.item.file.FlatFileItemReader

That's because the ScopedProxyMode is INTERFACES, and the proxy created is based on of the interfaces of FlatFileItemReader, and not FlatFileItemReader itself. Since we always have CGLIB when using @Configuration, I see no reason for not changing the default proxyMode of @StepScope to TARGET_CLASS.

One more argument for the proxyMode TARGET_CLASS: when I change the return type of the @Bean method to ItemReader<xx>, but still return a FlatFileItemReader, I don't get a ClassCastException, but the ItemStream registration does not work, and the FlatFileItemReader throws an exception on the first read.

Altogether those are serious stumble blocks for people not familiar with the proxying mechanisms in Spring, and we would avoid all of them with the default proxyMode TARGET_CLASS.


Affects: 2.2.0.RC1

Sub-tasks:

  • BATCH-2822 Class cast exception when autowiring flat file writer

Issue Links:

  • BATCH-1988 Refactor @StepScope to allow proxyMode = ScopedProxyMode.TARGET_CLASS
    ("duplicates")

Referenced from: commits 3780068

Backported to: 2.2.0.RC2

Metadata

Metadata

Assignees

No one assigned

    Labels

    has: backportsLegacy label from JIRA. Superseded by "for: backport-to-x.x.x"in: coretype: bug

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions