我試圖在 Spring 中實作單例設計模式。我在運行我的代碼時遇到錯誤。
我的組態檔是:
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("springdemo3")
public class AppConfig {
}
我創建了一個服務介面如下:
package springdemo3;
public interface Service {
void doSomething();
}
我創建了名為 Pump 的界面,如下所示;
public interface Pump {
void features();
}
創建了兩種型別的泵,電動和水泵都實作了泵介面,如下:
package springdemo3;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
@Component
public class ElectricPump implements Pump{
@Override
@Primary
public void features() {
System.out.println("This pump is an "
"electric pump");
}
}
package springdemo3;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
@Component
public class WaterPump implements Pump{
@Override
public void features() {
System.out.println("This pump is pumping"
" water");
}
}
我的 serviceB bean 創建如下:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component //This is a bean
public class ServiceB implements Service{
@Autowired
@Qualifier("electricpump")
private Pump mPump;
public Pump getMpump() {
return mPump;
}
public void setMpump(Pump mPump) {
this.mPump = mPump;
}
public void doSomething() {
System.out.println("We have a water"
" pump running here");
mPump.features();
}
}
雖然我的主要功能如下:
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/*** Here I'm not using XML configuration and using Singleton Design Pattern */
public class SpringDemo3 {
public static void main(String[] args) {
ApplicationContext context =
new AnnotationConfigApplicationContext(AppConfig.class);
ServiceB mServiceB = context.getBean(ServiceB.class);
mServiceB.doSomething();
}
}
當我運行它時,出現以下錯誤:
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'serviceB': Unsatisfied dependency expressed through field 'mPump'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'springdemo3.Pump' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier("electricpump")}
我正在嘗試了解 Spring 框架。請注意所有這些類都在同一個包中。我在這里做錯了什么?
但這有效

uj5u.com熱心網友回復:
請記住,對于Spring bean的默認命名convetion是駱駝的情況下,因此實際上為bean的正確名稱應該是electricPump代替electricpump。然后,您應該能夠自動裝配 bean。
除此之外,不建議現場自動裝配,因為它會使您的服務的單元測驗更加困難。最好使用建構式自動裝配。此外,您放置@Primary的位置不正確,因為它是將 bean 標記為自動裝配的首選的注釋。它可以在方法級別使用,但只有在方法定義 bean 并且也用注解進行@Bean注解時才有意義。
uj5u.com熱心網友回復:
正如我在評論中提到的,@Qualifier("electricpump")Pump 需要一個大寫的 P。
我創建了一個分支并向您的專案發出拉取請求:
- 重組你的專案
- 添加了一個maven pom.xml(因為我無法運行你的專案)
- 修復了 bean 名稱“electricPump”
考慮總是使用一些專案管理工具。通過這種方式,人們可以輕松地為您設定專案并幫助您。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/322715.html
