spring-hateoas: Application does not start due to two bean instances of org.springframework.plugin.core.PluginRegistry (Follow-up)
As explained in the last comment of #966 , an application using spring-hateoas-1.0.0 does not start if the bean factory is setup in a way that the ParameterNameDiscoverer is null or an alternative implementation.
The reason is that when autowiring of PluginRegistry into the bean _relProvider it finds two candidates and choosing one soley relies on the constructor argument name (same in EntityLinksConfigurationwhere the other instance of PluginRegistry is created.)
This could be easily resolved by using @Qualifier annotations and named beans.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 4
- Comments: 32 (3 by maintainers)
Add into SwaggerConfig @Bean public LinkDiscoverers discovers() {
Same here after migration to Spring-Boot 2.2.0.RELEASE.
I had the same issue but after adding this dependency everything is working
<dependency><groupId>org.springframework.plugin</groupId><artifactId>spring-plugin-core</artifactId><version>2.0.0.RELEASE</version></dependency>I did following workaround to avoid spring Hateoas startup issue Add following @Bean in the application starter class
Sorry, have not been in office for a couple of days and missed the discussion here.
I do not agree that this is related to spring fox. I don’t have spring fox on the classpath and can reproduce the issue.
The issue is that Spring HATEOAS relies on Spring autowiring fallback. If multiple candidate beans are found (here: multiple PluginRegistry beans) to be injected into another bean as constructor argument (here: relProvider), then Spring tries to guess the correct canidate by comparing bean names with constructor argument name. If any canidate matches this one is chosen.
However, it is possible to turn off this fallback in a spring application. One way is to set the
ParameterNameDiscovererto null in aBeanFactoryPostProcessorDoing this, the fallback mechanism is not able to resolve constructor arguments and can’t decide for a candidate. Instead, an exception is thrown.
Turning off the fallback is a valid configuration in our opinion if you want to avoid autowiring the wrong instance of a bean by chance. The term “fallback” is also used by Spring (see
org.springframework.beans.factory.support.DefaultListableBeanFactory, line 1537 .Fixing this on Spring HATEOAS side is pretty simple. Each PluginRegistry bean should have explicit names (e.g.
@Bean(name= relProviderPluginRegistry) and each constructor argument of type PluginRegistry should refer to exactly one of these names (e.g.@Qualifier("relProviderPluginRegistry")The workaround I have at the moment is to remove all
springfoxrelated dependencies in the project and I no longer get the above error.Following the same approach, but I just upgraded SpringFox version to
3.0.0, which fixed the issue as it started pulling latestspring-plugin-core. Thank you All.As @SimonHasak did, I solve this problem adding the dependency of spring core too.
Hi,
I am facing the same issue after trying to upgrade a project that relies on both Springfox and HATEOAS from Spring Boot 2.1 to Spring Boot 2.2. I see that @rainer198 suggested a solution within HATEOAS on October 30th. The ticket was then closed and the discussion fizzled out. But the problem remains.
Could @rainer198 's suggestion be incorporated into HATEOAS, @gregturn, or do you have any other suggestion on how this could be resolved (other than not upgrading at all or removing one of HateOAS or Springfox)? If I am forced to remove a library, it will most likely be HATEOAS as Springfox provides essential self-documentation of the REST API in the project.
Thanks
I ended up replacing springfox with the better maintained springdoc-openapi-ui. Problem solved.
On Thu, Mar 19, 2020, 10:09 Yurii Chekhotskyi notifications@github.com wrote:
@gregturn The reason to the error is that springfox uses an earlier version of spring-plugin which is not compatible with spring-hateoas which uses 2.0.0.RELEASE of the plugin. Tried to workaround this but realized it is not possible until springfox releases a new version using the latest spring-plugin version.
@gregturn Understand it should be the springfox community who is responsible to be compatible with latest Spring Boot 2.2.0.RELEASE and it’s subprojects. But while googling this it appears in your community.