消除歧义的@Primary和@Quelifier

@Primary

修改Bean的优先级

@Quelifier

  • 以类型和名称去寻找对应的Bean进行注入

@Autowired
@Qualifier("dog")
private Animal animal = null;
  • 带有参数的构造方法类的装配

package com.springboot.chapter3.pojo;

import com.springboot.chapter3.pojo.definition.Animal;
import com.springboot.chapter3.pojo.definition.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class BussinessPerson implements Person {
    
    private Animal animal = null;

    public BussinessPerson(@Autowired @Qualifier("cat")Animal animal) {
        this.animal=animal;
    }

    @Override
    public void Service() {
        this.animal.use();
    }

    @Override
    public void setAnimal(Animal animal) {
        this.animal=animal;
    }
}

猜你喜欢

转载自www.cnblogs.com/xl4ng/p/12678329.html
今日推荐