In a Spring Boot app, I have the following:
public interface Car {
⋮
}
@Component
public class OldCar implements Car {
⋮
}
@Component
@Primary
public class NewCar implements Car {
⋮
}
@Component
public class Driver {
@Autowired
private Car theCar;
⋮
}
(Note: Names of classes have been changed to protect company secrets. :-)
When I launch this app, it bombs with a NoUniqueBeanDefinitionException
, specifying:
Could not autowire field ... theCar; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException No qualifying bean of type [Car] is defined: expected single matching bean but found 2: newCar,oldCar
However, if I modify the type of theCar
to either OldCar
or NewCar
(to eliminate the ambiguity), everything works fine.
It seems to me that @Primary
is not having its intended effect. I am looking for ideas about what I can examine in order to discover potential causes of this unexpected behavior.
What could cause this? Why is @Primary
not working?