-
Notifications
You must be signed in to change notification settings - Fork 38.3k
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
Make bean initialization deterministic for multiple @Autowired
methods on same bean class
#30359
Comments
The order in which the |
Reproduced it on spring 6.0.8 |
@arvyy thanks for the reproducer. I am assuming you must be on Windows or something as I can easily reproduce it with MacOS without the loop and docker, see https://github.com/snicoll-scratches/SpringIssue30359 In general, you should not expose an interface type if the bean is to be injected by a specific subtype. Regardless of the outcome of this issue, I highly recommend fixing that. What's happening is that the bean initialization is not deterministic. If |
The root concern are the two separate To illustrate the case: a single The root cause for this is the JVM using arbitrary method ordering on reflection, returning methods in a different order between runs. We address this with ASM-based method declaration sorting for multiple |
@Autowired
methods on same bean class
Affects: spring 5.3 (specifically; spring boot 2.6.6)
Sorry, I realize this is a report for an old version, but maybe this is still relevant in newer versions because google wasn't of much help to me
After changing CI server to build on linux with docker, spring boot app started to undeterministically fail with NoSuchBeanDefinitionException. Ie, it works half the time (and it used to work all the time when the jar was being built on windows). I tracked it down and I think the cause was probably this:
ie., a bean is registered through interface type, and autowired through implementation type. I think this is the cause, because I stepped through wiring internals with debugger, and I found container rejecting makeBar bean due to
barImplType.isAssignableFrom(barType)
yielding false (where type is ResolvedType instance).This is bad code, but regardless if it should or shouldn't work, it should behave more deterministically.
The text was updated successfully, but these errors were encountered: