2012-06-13 111 views
0

我試圖創建一個bean,比試圖注入同樣在我的控制器,但我得到的bean創建失敗error.Here是我的代碼創建bean失敗(春季)

@Service("springSecurityLoginServiceImpl") 
public class SpringSecurityLoginServiceImpl implements SpringSecurityLoginService 
{ 
    //impl 
} 

這是怎麼了我試圖在我的控制器注入它

@Controller 
@RequestMapping("springSecurity/login.json") 
public class SpringSecurityLoginController 
{ 
    @Autowired 
    @Qualifier("springSecurityLoginServiceImpl") 
    SpringSecurityLoginService springSecurityLoginService; 

} 

有除了這些註解Spring的MVC-配置xml文件中沒有條目,但是當我開始服務器面臨以下異常

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0' 
defined in ServletContext resource [/WEB-INF/config/spring-mvc-config.xml]: 
Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'springSecurityLoginController': 
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
Could not autowire field: com.core.servicelayer.user.SpringSecurityLoginService com.storefront.controllers.pages.SpringSecurityLoginController.springSecurityLoginService; 
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No matching bean of type [com.core.servicelayer.user.SpringSecurityLoginService] found for dependency: 
expected at least 1 bean which qualifies as autowire candidate for this dependency. 
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), 
@org.springframework.beans.factory.annotation.Qualifier(value=springSecurityLoginServiceImpl)} 

我不知道我做錯了還是什麼額外的我必須做

+1

你可以在spring-mvc-config.xml中顯示你正在使用的組件掃描標籤嗎? – gkamal

回答

1

SpringSecurityLoginController類是指SpringSecurityLoginService類,是沒有定義的bean。錯誤說的太多了。

確實如此,因爲您只爲LoginServiceImpl類定義了一個bean,它似乎沒有以任何方式擴展SpringSecurityLoginService

Spring的bean查找算法首先搜索哪些類型爲或擴展的bean,其中包括SpringSecurityLoginService。然後,使用Qualifier縮小可用選項。在這種情況下,沒有bean是首先發現...

Spring doc

4.11.3微調基於註解的自動連接與預選賽

由於按類型自動裝配可能會導致多個候選人,通常需要對選擇過程有更多的控制權。 完成此操作的一種方法是使用Spring的@Qualifier註釋。這允許 將限定符值與特定參數相關聯,縮小了匹配類型集合的範圍,從而爲每個參數 選擇一個特定的bean。

例如,您需要LoginServiceImpl將執行SpringSecurityLoginService

編輯

因爲它只是你可能不包括SpringSecurityLoginService的包component-scan標籤,在Spring配置文件(如gkamal已經提到)一個錯字。你應該有什麼樣:

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

其中org.exampleSpringSecurityLoginService的包進行更換。

+0

我錯誤,因爲我只是在我的post.Please錯誤做了一些錯誤。請參閱更新後發佈'SpringSecurityLoginService'由'SpringSecurityLoginServiceImpl'實現 –