SSM-Spring-Spring装配Bean-通过注解装配Bean-自动装配的歧义性(@Primary和@Qualifie)

SSM-Spring-Spring装配Bean-通过注解装配Bean-自动装配的歧义性(@Primary和@Qualifie)


注解@Primary

​ 代表首要的,让容器通过一个接口或者抽象类注入对象,由于存在多个实现类或具体类,倒是无法判断。该注解告诉容器,优先使用该类注入

@Component("roleService3")
@Primary
public class RoleServiceImpl3 implements RoleService{
    
    
    //...
}

​ 代表了,如果存在多个RoleService类型,无法判断注入哪个,优先使用RoleServiceImpl3的实例注入。无论怎样改注解只能解决首要性的问题,不能解决选择性问题,


注解@Qualifie

​ 是一个按名称查询的方法:

    @Autowired
    @Qualifier("source")
    private Role role=null;

这时候,容器不会按照类型注入,而是按照名称的方式注入

猜你喜欢

转载自blog.csdn.net/weixin_43958223/article/details/115084366