Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve documentation of scoped beans definition #1502

Closed
spring-projects-issues opened this issue Sep 8, 2013 · 5 comments
Closed

Improve documentation of scoped beans definition #1502

spring-projects-issues opened this issue Sep 8, 2013 · 5 comments
Labels
for: backport-to-4.3.x Issues that will be back-ported to the 4.3.x line in: documentation type: enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Sep 8, 2013

@spring-projects-issues
Copy link
Collaborator Author

Simon Wong commented

If JavaConfig being used (no such issue for using xml config), adding @StepScope to ItemWriter for FlatFileItemWriter will throw the Exception "org.springframework.batch.item.WriterNotOpenException: Writer must be open before it can be written to" during job execution.

    @Bean
    @StepScope
    public ItemWriter<String> writer() {
        FlatFileItemWriter<String> itemWriter = new FlatFileItemWriter<String>();

        itemWriter.setResource(new FileSystemResource("c:/some_output.csv"));
        itemWriter.setLineAggregator(new PassThroughLineAggregator<String>());

        return itemWriter;
    }

@spring-projects-issues
Copy link
Collaborator Author

Tobias Flohre commented

It will work when the method writer returns the FlatFileItemWriter as type. JavaConfig does not recognize that the writer is an ItemStream as well if the return type is ItemWriter. But anyway, it would be nice if it would work without that workaround.

@spring-projects-issues
Copy link
Collaborator Author

Michael Minella commented

Per Tobias Flohre, ItemWriter doesn't have an open method...ItemStream does so there is nothing to open. If you return an ItemStream, or an ItemStreamWriter, it would should fine.

@spring-projects-issues
Copy link
Collaborator Author

Tobias Flohre commented

One more comment to make it clearer: when a bean is step-scoped, it does not get instantiated on startup, instead a cglib proxy based on the return type of the @Bean method is created. If the return type is ItemWriter, the proxy does not implement ItemStream and therefore the writer doesn't get registered as one. That's why the open method never gets called.
There's no way around it: the return type has to implement ItemStream.

@spring-projects-issues
Copy link
Collaborator Author

Simon Wong commented

Thanks both of you.
It would be nice to add a caveat in the reference document.

@fmbenhassine fmbenhassine changed the title FileItemWriter throws WriterNotOpenException exception if JavaConfig and @StepScope is used for writer [BATCH-2097] Improve documentation in regards to the return type of reader/writer bean definition methods Jan 17, 2020
@fmbenhassine fmbenhassine added this to the 5.0.0 milestone Dec 13, 2021
@fmbenhassine fmbenhassine added for: backport-to-4.3.x Issues that will be back-ported to the 4.3.x line and removed status: waiting-for-triage Issues that we did not analyse yet labels Dec 13, 2021
@fmbenhassine fmbenhassine modified the milestones: 5.0.0, 5.0.0-M7 Sep 22, 2022
@fmbenhassine fmbenhassine changed the title Improve documentation in regards to the return type of reader/writer bean definition methods Improve documentation of scoped beans definition Sep 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: backport-to-4.3.x Issues that will be back-ported to the 4.3.x line in: documentation type: enhancement
Projects
None yet
Development

No branches or pull requests

2 participants