Spring @Primary
Sometimes we need to define multiple beans of the same type
. In these cases, the injection will be unsuccessful because Spring has no clue which bean we need.
方案1:@Qualifier
方案2:@Primary
@Component
@Primary
class Car implements Vehicle {
}
@Component
class Bike implements Vehicle {
}
@Component
class Driver {
@Autowired
Vehicle vehicle;
}
@Component
class Biker {
@Autowired
@Qualifier("bike")
Vehicle vehicle;
}
还没有评论,来说两句吧...