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
Injecting beans from auto-configuration by their actual type fails #32763
Comments
As far as I know, it has always been possible for this to happen. Your It's possible that a change somewhere (I suspect Spring Framework to be the most likely place) has made it more likely that
|
TIL. Thanks! It never encountered this problem before and it happened when upgrading to Spring Boot 3.0. Does this advice relates only to beans created in auto-configurations? Because code samples where the interface is a return type are even in Spring Framework docs (https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-java-basic-concepts). Btw other Spring projects in few places make the same mistake as I did. For example:
|
It's particularly important for auto-configuration and bean conditions as the conditions are evaluated before beans are created. That means that the only type information that's available is from the signature of the In terms of dependency injection, the problem can occur in any Spring app. It depends entirely on the order in which the beans are created. Without explicit or implicit dependencies between beans, the ordering isn't really guaranteed and the problem can occur if the beans are created in the "wrong" order. Thanks for spotting the problem with
Here's an example from the Framework docs:
This isn't necessarily wrong. It depends on whether or not you want to be able to inject From the Framework docs again:
Arguably, this is wrong. In the XML case, the bean factory will know that the bean's type is |
Thanks a lot @wilkinsona for in-depth explanation! |
One more note on AOT/Native. If you don't expose the type in the type signature, then it is impossible for the AOT engine to introspect the actual type of the bean at build-time. This may or may not be a problem depending on the feature you're using. For instance, if you have a |
Lets assume following auto-configuration class:
Injecting
personService
bean byPersonServiceImpl
type:fails with:
Injecting through an interface type works without problems. Also, injecting by actual type (
PersonServiceImpl
) works for test classes andapplicationContext.getBean(PersonServiceImpl.class)
returns an actual bean.As far as I can see only beans created through auto-configurations are affected. Beans created through regular configurations are injectable through their interfaces and actual classes.
This behaviour has not been observed in Spring Boot 2.7.4 but is reproducible from Spring Boot 3.0.0-M3 (I am not able to run build with older milestones due to missing dependencies).
Sample project that reproduces this issue coming soon.Sample project that reproduces this issue: https://github.com/maciej-scratches/spring-boot-gh-32763 (
./mvnw verify
to see the failing test).The text was updated successfully, but these errors were encountered: