您现在的位置是:首页 >其他 >Spring @Primary网站首页其他

Spring @Primary

张紫娃 2024-06-17 10:13:55
简介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.

We already saw an option to deal with this scenario: marking all the wiring points with @Qualifier and specify the name of the required bean.

However, most of the time we need a specific bean and rarely the others. We can use @Primary to simplify this case: if we mark the most frequently used bean with @Primary it will be chosen on unqualified injection points:

@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;
}

In the previous example Car is the primary vehicle.
Therefore, in the Driver class, Spring injects a Car bean.
Of course, in the Biker bean, the value of the field vehicle will be a Bike object because it’s qualified.


风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。