Hello,
I'm facing a problem during the upgrade to spring boot 2.1.0
We have lots applications with a depdency to a module that is overwriting an existing spring bean. After the upgrade to spring-boot 2.1.0 the applications cannot start anymore because spring.main.allow-bean-definition-overriding=true has to be set.
So in order to fix this issue I added "spring.main.allow-bean-definition-overriding=true" to a property file inside the modul. The Property file is loaded by a Configuration class with @PropetySource annotation.
The @Configuration Class is also inside the module.
But that did not work. The applications still fail to start with the same error. It only works if I add pring.main.allow-bean-definition-overriding=true to the application.properties of the application.
But this is not a good solution because we have to change all our applications and not a single module. I guess this is a Bug or?
The behaviour you've described is expected. Properties contributed by @PropertySource
are not available until the application context is being refreshed, by which point it's too late to honour spring.main.allow-bean-definition-overriding
. If you want to set a property globally, I would recommend using an EnvironmentPostProcessor
registered via spring.factories
.
Thanks for your answer. It worked.
If anyone else has the same problem:
Create a Class, implement EnvironmentPostProcessor and set the property like this
@Override
public void postProcessEnvironment(final ConfigurableEnvironment environment, final SpringApplication application) {
application.setAllowBeanDefinitionOverriding(true);
}
Also create a file spring.factories in META-INF Folder and register your class like this
org.springframework.boot.env.EnvironmentPostProcessor=package.name.YourClassname
@DiabolusExMachina Thanks for following up. While that will meet your needs, it's not quite what I had in mind. I was trying to point you in the direction of adding a PropertySource
to the environment that contains a spring.main.allow-bean-definition-overriding=true
key-value pair.
@wilkinsona could you please mention what is the other way that this problem can be solved - you were pointing at something?
When I follow the approach given by @DiabolusExMachina and try to load the application in a test context, the bean overriding is allowed but for some reason after that the configuration classes annotated with @Conditional beans start malfunctioning - either all the beans load or non of them load. The property loading mechanism seems to have been altered in some way
Rather than calling setAllowBeanDefinitionOverriding
on the SpringApplication
, the EnvironmentPostProcessor
implementation should add a PropertySource
, probably a new MapPropertySource
, to the environment. That PropertySource
should contain a spring.main.allow-bean-definition-overriding
property with the value true
.
If you have any further questions, please follow up on Gitter or Stack Overflow.
Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.
Most helpful comment
The behaviour you've described is expected. Properties contributed by
@PropertySource
are not available until the application context is being refreshed, by which point it's too late to honourspring.main.allow-bean-definition-overriding
. If you want to set a property globally, I would recommend using anEnvironmentPostProcessor
registered viaspring.factories
.