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.