spring-projects / spring-projects/spring-batch
Duplicate JobScope beans created when loading XML based bean definitions with SpringApplication.setSources
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 3k
- Forks
- 2.5k
- Avg merge
- 6d 53m
- Merged PRs (30d)
- 3
Description
This is similar to #3936 with the distinction that this happen for JobScope beans.
Coming to the issue, the below spring boot 2 setup (see reproducer below) throws a BeanDefinitionOverrideException during startup only when configuring the beans using XML based bean definitions with the SpringApplication.setSources method same works while using Java based bean definition or while using the same XML in `@ImportResource.
Reproducer:
https://github.com/anthochristen/testing-spring-batch
Error:
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean 'scopedTarget.reader', defined in BeanDefinition defined in org.example.Main, could not be registered. A bean with that name has already been defined in org.example.Main and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
Process finished with exit code 1
What I see
I see that the step scope bean poxy scopedTarget.reader is registered twice in the Bean definition registry once while Spring does loads the Main config class and second again when the JobScope post processor (PP) is invoked and it doesn't recognise the existing scoped proxy created during load of the first source, causing this error.
While using Java based configs the JobScope Bean PP has the auto proxy flag set to false (in ScopeConfiguration class), so it doesn't proxy the bean. And while using ImportResource way the XML definition is only parsed in refreshContext after the ScopeConfiguration is initialised so it works as expected.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked reproducer and compare SpringApplication.setSources with @ImportResource. Read ScopeConfiguration and the JobScope post processor, focusing on how the scopedTarget.reader proxy is registered in each setup. Done means the XML-based configuration starts without registering the same JobScope bean twice.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring, spring-boot
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100