【问题标题】:Qualifier not working in spring预选赛在春季不起作用
【发布时间】:2017-02-15 09:37:56
【问题描述】:

CallingApp.java

@Service
@ComponentScan(basePackages = { "com.codegeekslab.type" })
public class CallingApp {

    @Autowired
    @Qualifier("BasicPhone")
    private Phone phone;

    public CallingApp(Phone phone) {
        this.phone = phone;
    }

    public void makeCall(int number) {
        phone.openApp(number);
    }

}

电话.java

package com.geekslab.device;

public interface Phone {

    public void openApp(int number);

}

BasicPhone.java

package com.codegeekslab.type;

import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.geekslab.device.Phone;
@Component("BasicPhone")
 public class BasicPhone implements Phone {
    {
        System.out.println("BasicPhone");
    }

    public void openApp(int number) {
        System.out.println("calling via simcard... " + number);
    }

}

SmartPhone.java

package com.codegeekslab.type;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.geekslab.device.Phone;

@Component("SmartPhone")
public class SmartPhone implements Phone {
    {
        System.out.println("SmartPhone");
    }

    public void openApp(int number) {
        System.out.println("calling via whatsapp..." + number);
    }

}

Test.java

package com.codegeekslab.test;

 import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.codegeekslab.app.CallingApp;
import com.codegeekslab.type.BasicPhone;
import com.codegeekslab.type.SmartPhone;
import com.geekslab.device.Phone;

 public class Test {

    public static void main(String[] args) {

        //ApplicationContext context =
        //      new GenericXmlApplicationContext("beans.xml");
        //SpringHelloWorld helloSpring  = context.getBean("springHelloWorld", SpringHelloWorld.class);
        //comment this for xml less spring 
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
         context.scan("com.codegeekslab.app","com.codegeekslab.type");
        //context.register( BasicPhone.class,SmartPhone.class,CallingApp.class);
         context.refresh();
        CallingApp  callingApp  = context.getBean("callingApp", CallingApp.class);  
        callingApp.makeCall(99999);

    }
}

即使我在CallingApp 类中将限定符指定为@Qualifier("BasicPhone"),但我得到的Exception如下:

没有定义 [com.geekslab.device.Phone] 类型的合格 bean:预期单个匹配 bean,但找到 2:BasicPhone,SmartPhone

【问题讨论】:

  • 你能从你的服务类中删除@ComponentScan(basePackages = { "com.codegeekslab.type" })然后试试吗??
  • @SachinSarawgi 得到了答案,无论如何

标签: java spring autowired qualifiers


【解决方案1】:

您在 CallingApp 服务中将 phone 作为构造函数参数传递,而不指定 bean。

尝试在您的构造函数中放置一个限定符,或者坚持使用您已经在做的自动装配注入。

【讨论】:

    【解决方案2】:

    我删除了 CallingApp 类构造函数,它工作了。

     public CallingApp(Phone phone) {
                    this.phone = phone;
                }
    

    由于构造函数覆盖了 setter 方法。

    【讨论】:

      【解决方案3】:

      你不需要添加任何参数构造函数

      public CallingApp(){
          //do nothing
      }
      public CallingApp(Phone phone) {
          this.phone = phone;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-22
        • 2015-02-27
        • 2016-08-20
        相关资源
        最近更新 更多