Skip to content

Make bean initialization deterministic for multiple @Autowired methods on same bean class #30359

@arvyy

Description

@arvyy

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:

@Service
public class Foo {
  @Autowired public void setBar(BarImpl bar) { ... }
}

@Configuration
public class Config {
  @Bean 
  public Bar makeBar() {
    return new BarImpl();
  }
}

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.

Metadata

Metadata

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: backportedAn issue that has been backported to maintenance branchestype: enhancementA general enhancement

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions