-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Non-helpful NoSuchBeanDefinitionException rather than BeanNotOfRequiredTypeException due to creation order [SPR-14504] #19073
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Juergen Hoeller commented Bean initialization is generally deterministic from the core container's perspective, since we're always approaching it in registration order. However, with In any case, I'll see what we can do here towards better reporting at least. |
Juergen Hoeller commented Running the test above locally, it indeed is the |
Andy Wilkinson commented
Wow. I'd assumed it perhaps varied based on the ordering of the byte code so it'd only change upon recompilation.
That's a change that I would like to see. Perhaps it could be considered for 5.0? |
Juergen Hoeller commented Indeed, see #19074 :-) I'll keep this issue for the exception reporting improvement in such a scenario... which we should generally aim for (since even manual ordering may easily lead to such a situation) and which we'll backport to 4.3.2. |
Juergen Hoeller commented An important note: Bean registration order deterministically follows source-code method declaration order with ASM parsing!
As a consequence, the following variation of your sample code above provides consistent results for me:
(As a side note, for Spring exceptions, you can catch So it seems that regular classpath scenarios - including regular Boot usage - get reliable results anyway, and that only |
Juergen Hoeller commented I've addressed this with a general check for such bean target type mismatches, leading to a This will be helpful for configuration class ordering issues but also for explicit ordering with the proxied bean definition coming first and a bean with a corresponding injection point later on. |
Andy Wilkinson opened SPR-14504 and commented
With reference to #19047, I am trying to add a
FailureAnalyzer
to Spring Boot that outputs some advice when refresh fails due to aBeanNotOfRequiredTypeException
. In doing so I have observed an unpleasant side-effect of bean creation ordering not being deterministic. Sometimes refresh will fail with aBeanNotOfRequiredTypeException
as the root cause and other times the root cause will be aNoSuchBeanDefinitionException
. When it's the latter there is no information to help you figure out why there was no such bean.Here's a small class that demonstrates the problem caused by the non-deterministic ordering:
If you run it a few times you should see the two different root causes. The key thing is the order in which
asyncBean
anduser
are created.If
asyncBean
is created first its proxy is stored in the bean factory and, subsequently, the attempt to find anAsyncBean
instance for injection into theuser
bean method fails as there's no matching bean.If
user
is created first then its creation triggers the creation ofasyncBean
. I think this means thatasyncBean
is directly available to be passed into theuser
bean method rather than requiring a bean factory lookup. When this fails as the types don't actually match aBeanNotOfRequiredTypeException
is thrown with details of the actual type.Ideally, the ordering would be deterministic, but I realise that's almost certainly not possible in 4.3 and perhaps at all. Failing that, some information in the
NoSuchBeanDefinitionException
that points towards the proxiedasyncBean
would be very useful.Affects: 4.3.1
Reference URL: spring-projects/spring-boot#6434
Issue Links:
@Bean
registration order within Class-reflected configuration classes@Bean
methodsThe text was updated successfully, but these errors were encountered: