簡體   English   中英

如果所有類不在同一個包中,則Spring @autowired不起作用

[英]Spring @autowired do not work if all classes are not in the same package

我有四個包:

  1. com.spring.org

    文件: HomeController.java

  2. com.spring.org.dao

    文件: SubscriberDao.javaSubscriberDaoImpl.java

  3. com.spring.org.model

    文件: Subscriber.java

  4. com.spring.org.service

    文件: SubscriberService.javaSubscriberServiceImpl.java

我將所有控制器類放在com.spring.org包中,其他包基於其類型放在不同的包中。 如果我運行我的應用程序,我收到此錯誤消息:

HTTP狀態500 - servlet appServlet的Servlet.init()拋出異常 沒有找到依賴的類型[com.spring.org.service.SubscriberService]的限定bean:預期至少有一個bean有資格作為此依賴關系的autowire候選者... ..

僅供參考:我在我的控制器中使用自動裝載的annoation,如下所示:

@Autowired
private SubscriberService subService;

但是,如果我將所有類和接口放在com.spring.org包中,那么我的應用程序就可以完美運行。

我已經嘗試在我的servlet-context.xml文件中使用這些標記來解決問題,但它仍然無效:

<annotation-driven />
<context:annotation-config />
<context:component-scan base-package="com.spring.org.**" />
<context:component-scan base-package="com.spring.org.dao" />
<context:component-scan base-package="com.spring.org.model" />
<context:component-scan base-package="com.spring.org.service" />

我也只嘗試過這個:

<context:component-scan base-package="com.spring.org" />

你可以在這里看到我的servlet-context.xml文件的代碼http://postimg.org/image/s6bnjccrn/

你能告訴我如何解決這個問題嗎?

如果您需要查看任何其他文件,請告訴我。

更新

我的SubscriberService代碼:

@Service
public interface SubscriberService {

 public void addSubscriber(Subscriber subscriber);
 public void updateSubscriber(Subscriber subscriber);
 public Subscriber getSubscriberById(int subId);
 public List<Subscriber> listSubs();
 public int removeSubscriber(int subId);    

}

根本原因

org.springframework.beans.factory.BeanCreationException:創建名為'homeController'的bean時出錯:注入自動連接的依賴項失敗; 嵌套異常是org.springframework.beans.factory.BeanCreationException:無法自動裝配字段:private com.spring.service.SubscriberService com.spring.org.HomeController.subService; 嵌套異常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到[com.spring.service.SubscriberService]類型的限定bean用於依賴:預期至少有1個bean可以作為此依賴項的autowire候選者。 依賴注釋:{@ org.springframework.beans.factory.annotation.Autowired(required = true),@ org.springframework.beans.factory.annotation.Qualifier(value =)} org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor .postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)

如果我在這里粘貼所有代碼,那將非常難以理解,所以我在這里上傳了我的整個項目https://www.mediafire.com/?crxe7vt7uwyqwtl 我正在使用Eclipse IDE。

你的結構應該是這樣的

訂閱服務接口

package com.spring.org.service;

public interface SubscriberService {

}

SubscriberServiceImpl.java

package com.spring.org.service;

@Component
@Qualifier("Subscriber")
public class SubscriberServiceImpl implements SubscriberService {

}

'SubscriberServiceImpl1'是一個組件,它實現了'SubscriberService'。

SubscriberServiceImpl1.java

package com.spring.org.service;

@Component
@Qualifier("Subscriber1")
public class SubscriberServiceImpl1 implements SubscriberService {

}

我設置了一個Spring上下文,它掃描這兩個包中標有'@Component'的bean。

servlet的context.xml中

<annotation-driven />
<context:annotation-config />
<context:component-scan base-package="com.spring"/>

HomeController.java

@Controller
public class HomeController {

    @Autowired
    @Qualifier("Subscriber")
    private SubscriberService subService;

}

請參閱此鏈接 希望對你有幫助....

編輯

根據您的包結構,您的SubscriberServiceImpl類位於包com.spring.org.service下,只需使用com.spring更改您的基本包,這將適合您

<context:component-scan base-package="com.spring"/>

問題是您有多個SubscriberService接口的實現。

當您編寫以下代碼時:

@Autowired
private SubscriberService subService;

Spring將尋找SubscriberService的實現,因為你將有多個實現,spring將不知道要注入哪個實現。

解決方案是使用@Qualifier來區分不同的實現。

有關@Qualifier更多信息和演示,請訪問鏈接。

或者,如果您有一個SubscriberService實現,請確保服務和實現都屬於您在spring上下文中提供的掃描包。

希望能幫助到你。

您只需指定基礎包:

<context:component-scan base-package="com.spring.org"/>

我相信你應該注釋實現類而不是接口。

嘗試使用逗號分隔包,如下所示:

<context:component-scan 
base-package="com.spring.org,com.spring.org.dao,com.spring.org.model,com.spring.org.service" />

嘗試在SubscriberServiceImpl上添加@Component。

基本上注釋如@ Service,@ Repository,@ Component等,它們都有相同的用途:

使用基於注釋的配置和類路徑掃描時自動檢測。

根據我的經驗,我總是在接口或抽象類和注釋(如@Component和@Repository)上使用@Service注釋來實現它們。 @Component注釋我在那些用於基本目的的類上使用,簡單的Spring bean,僅此而已。 我正在DAO層中使用的@Repository注釋,例如,如果我必須與數據庫通信,進行一些事務等。

如下指定基本掃描並從接口中刪除注釋,並僅保留在實現類中,例如@ Service,@ Repository,@ Component等。

<context:component-scan base-package="com.spring.org"/> 

編輯:

我查看了你的代碼。你已經將組件掃描為

但是您的SubscriberService.java位於com.spring.service包中。 請將軟件包更改為com.spring.org.service。

首先,您必須將此標記放在XML(應用程序上下文文件)中:

<context:component-scan base-package="com.spring.org"/>

為了解決這個問題,你需要改變這個:

@Autowired
@Qualifier("Subscriber")
private SubscriberService subService

因為Spring會搜索SubscriberService類型的bean(在你的例子中),如果它找到了這樣的bean,它會將它注入到這個方法中。 如果它找到兩個這樣的bean,你將得到一個Exception(它與你的堆棧跟蹤中的相同)。

如果您不想使用兩個注釋( @Autowired@Qualifier ),您可以使用@Resource來組合這兩個:

@Resource(name="redBean")
private SubscriberService subService

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM