Problem with Autowiring & No unique bean

后端 未结 3 1772
执笔经年
执笔经年 2020-12-10 02:52

I have 2 classes (B,C) extends class A.

@Service
public class A  extends AbstratClass{

    @Autowired
    A(MyClass  br) {
        super(br);
         


        
3条回答
  •  有刺的猬
    2020-12-10 03:31

    Generally you will get this error when defined two beans with same class

    
    
    

    if you address the above two line we have two beans with same class.

    when you trying to autowire this class in any other classed you will get this type of error

    You have two solutions

    First Method

    1. use qualifier by defining a bean id init like this

      @Autowired
      @Qualifier("a")
      MyClass a;
      
      @Autowired
      @Qualifier("b")
      MyClass b;
      

    Second Method

    use JSR250 api(its a jar file you can put into your class path

    Then do autowriring like below

    @Resource("a")
    MyClass a
    
    @Resource("b")
    MyClass a
    

提交回复
热议问题