You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Lets say we have 2 beans that implement interface A:
interface A {}
class B implements A {}
@Configuration
public class C {
@Bean public A red() { return new B(); }
@Bean public A black() { return new B(); }
}
If we autowire them later, in 2 fields, we won't get an exception.
But, we will randomly get same bean injected in both fields.
@Service
public class ServiceImpl {
private final A red;
private final A black;
public ServiceImpl(A red, A black) {
this.red = red;
this.black = black;
}
}
Otherwise, if we change type of fields (from interface to class), it will work as expected.
@Service
public class ServiceImpl {
private final B red;
private final B black;
public ServiceImpl(B red, B black) {
this.red = red;
this.black = black;
}
}
I can't reproduce this: Such an arrangement injects fine for me, selecting the target beans by type first and then matching each bean by name against the parameter name in a second pass. Could you please double-check your effect and provide a corresponding unit test that reliably fails for you in the interface case but works in the target class case?
FWIW, we generally recommend to be as specific as possible in your @Bean return type signature, i.e. declaring B as a return type in your scenario. This allows the container to introspect the implementation type early, not just learning about it once the bean is actually created.
Uh oh!
There was an error while loading. Please reload this page.
Max Samsonov opened SPR-16174 and commented
Lets say we have 2 beans that implement interface A:
If we autowire them later, in 2 fields, we won't get an exception.
But, we will randomly get same bean injected in both fields.
Otherwise, if we change type of fields (from interface to class), it will work as expected.
Affects: 4.3.12
Issue Links:
@Autowired
matches several beans by typeThe text was updated successfully, but these errors were encountered: