java - 限定符在 Spring 不起作用

标签 java spring autowired qualifiers

调用App.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);
    }

}

智能手机.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);
    }

}

测试.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") 提供,我也会收到如下异常:

No qualifying bean of type [com.geekslab.device.Phone] is defined: expected single matching bean but found 2: BasicPhone,SmartPhone

最佳答案

您在 CallingApp 服务中将电话作为构造函数参数传递,但未指定 bean。

尝试在您的构造函数中放置一个限定符,或者坚持使用您已经做过的 Autowiring 注入(inject)。

关于java - 限定符在 Spring 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42245597/

相关文章:

java - 引用另一个 .xml 中定义的 bean

Spring Boot - Autowiring 数据源 Bean

spring - Eclipse 无法识别@Inject?

java - 数组的所有可能组合

java - 停止带百分号的 DecimalFormat 移动小数点

spring - OAuth2多重身份验证中的空客户端

java - Spring @Transactional 方法错误处理

unit-testing - IntelliJ 中 Spring Boot 的 Bean 扫描

java - 如何使用 AbstractDataModel 过滤 JTable?

java - Android 绑定(bind)策略,适配器应该做工作,还是 View ?