Java 春天不工作
CallingApp.javaJava 春天不工作,java,spring,autowired,qualifiers,Java,Spring,Autowired,Qualifiers,CallingApp.java @Service @ComponentScan(basePackages = { "com.codegeekslab.type" }) public class CallingApp { @Autowired @Qualifier("BasicPhone") private Phone phone; public CallingApp(Phone phone) { this.phone = phone;
@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);
}
}
Phone.java
package com.geekslab.device;
public interface Phone {
public void openApp(int number);
}
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);
}
}
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);
}
}
BasicPhone.java
package com.geekslab.device;
public interface Phone {
public void openApp(int number);
}
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);
}
}
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);
}
}
SmartPhone.java
package com.geekslab.device;
public interface Phone {
public void openApp(int number);
}
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);
}
}
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”)
的形式给出了限定符,但我得到的异常如下所示:
未定义[com.geekslab.device.Phone]类型的合格bean:预期为单个匹配bean,但找到2:BasicPhone,SmartPhone
您在CallingApp服务中将phone作为构造函数参数传递,而不指定bean
尝试在构造函数中添加一个限定符,或者坚持使用autowire注入,这是您已经做过的事情。您在CallingApp服务中将phone作为构造函数参数传递,而不指定bean
尝试在构造函数中添加限定符,或者坚持使用autowire注入,这是您已经做过的事情。我删除了CallingApp类构造函数,它成功了
public CallingApp(Phone phone) {
this.phone = phone;
}
因为构造函数重写了setter方法。我删除了CallingApp类构造函数,它工作了
public CallingApp(Phone phone) {
this.phone = phone;
}
因为构造函数重写了setter方法。您不需要添加参数构造函数
public CallingApp(){
//do nothing
}
public CallingApp(Phone phone) {
this.phone = phone;
}
您不需要添加参数构造函数
public CallingApp(){
//do nothing
}
public CallingApp(Phone phone) {
this.phone = phone;
}
你能从你的服务类中删除
@ComponentScan(basePackages={“com.codegeekslab.type”})
吗?然后试试看???@SachinSarawgi得到了答案,无论如何thnaks你能从你的服务类中删除@ComponentScan(basePackages={“com.codegeekslab.type”})
,然后试试看???@SachinSarawgi得到了答案,无论如何thnaks