我在創建一個用於依賴注入的bean時遇到了一個問題。這是場景。bean的實例化失敗:指定的類是一個接口
我正在處理MongoDB存儲庫,我也創建了一個使用它的類。我試圖實例化兩者的bean實例。
MongoDB的reporsitory:
@Repository
public interface ProductGlobalTrendRepository extends MongoRepository<ProductGlobalTrend,String>{
public ProductGlobalTrend findByPid(@Param("pId") String pId);
}
種類,其在使用它:
@Service
@Scope("singleton")
public class ProductTrendService {
@Autowired
@Qualifier("productGlobalTrendRepo")
ProductGlobalTrendRepository productGlobalTrendRepo;
public ProductTrendService() {
super();
}
public void setProductGlobalTrendRepo(
ProductGlobalTrendRepository productGlobalTrendRepo) {
this.productGlobalTrendRepo = productGlobalTrendRepo;
}
public ProductTrendService(ProductGlobalTrendRepository productGlobalTrendRepo) {
super();
this.productGlobalTrendRepo = productGlobalTrendRepo;
}
}
彈簧的Bean配置XML有這些項:
<bean id="productTrendService" class="com.api.services.ProductTrendService"> </bean>
<bean id="productGlobalTrendRepo" class="com.mongodb.repository.ProductGlobalTrendRepository"> </bean>
以下是我的錯誤得到:
19428 [本地主機-startStop-1] WARN org.springframework.web.context.support.AnnotationConfigWebApplicationContext - 上下文初始化過程中遇到異常 - 取消刷新嘗試 org.springframework.beans.factory.BeanCreationException:錯誤 創建豆名稱爲'productTrendService':注入自動線 依賴關係失敗;嵌套的異常是 org.springframework.beans.factory.BeanCreationException:不能 autowire字段:com.mongodb.repository.ProductGlobalTrendRepository com.api.services.ProductTrendService.productGlobalTrendRepo;嵌套0例外是org.springframework.beans.factory.BeanCreationException:與名稱 錯誤創建豆「productGlobalTrendRepo」在 類路徑資源定義[COM/VSTORE/CONF /彈簧的security.xml]: 實例化豆的失敗;嵌套的異常是 org.springframework.beans.BeanInstantiationException:無法 實例化[com.mongodb.repository.ProductGlobalTrendRepository]: 指定類是一個接口
它抱怨存儲庫是一個接口類。
有人可以爲這個bean依賴注入建議一個修復/解決方法嗎?
感謝您對amitj的快速響應。然而,我想出了一個解決方法,而不是在構造函數中設置bean,我使用@PostConstruct註釋構建初始化。它的工作。 –