huehnerlady opened SPR-17519 and commented
We like the idea that with spring boot 2.1.0 the default value for the property
spring.main.allow-bean-definition-overridingis now set to false.
But this comes with a challenge. We have tests which override one specific bean with a mock for integration testing. Now for these tests we get a BeanOverrideException.
We currently use the workaround with setting the property spring.main.allow-bean-definition-overriding
to true
for these specific tests, but in the end we might miss other overridden beans in a test which makes the app fail to start later. So we have to add another (expensive) Integration test that checks whether the app starts up without any errors.
So my proposal is to add an annotation - kind of similar to @Primary
, that says that you actively want to override ONE SPECIFIC bean and make THIS bean primary.
Other idea is to just have an annotation to allow ONE SPECIFIC bean to be overridden and add the @Primary
annotation as well.
What do you think?
Reference URL: https://github.com/spring-projects/spring-boot/issues/15216#event-1975334888
Comment From: sbespalov
we had the same issue after switching to Spring Boot v.2.1.0, the solution was to rename beans, that we have in test environment.
So the @Primary
annotation still works with autowiring by type, if there is no bean name conflicts.
Comment From: dmurat
Beside annotation, I think it will be useful to expand allow-bean-definition-overriding
with an additional suffix, i.e. names
, which will allow specifying a list of bean names which are allowed to be overridden.
Comment From: snicoll
Unfortunately, we're leading towards deprecating bean definition overriding altogether, see #31288.