簡體   English   中英

Spring沒有類型匹配的bean,預計至少有1個bean

[英]Spring No matching bean of type, expected at least 1 bean

得到錯誤

 No matching bean of type [foo.bar.service.AccountService] 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)}

我的服務:

public interface AccountService {

@Service
public class AccountServiceImpl implements AccountService {

所以我應該得到實施

我試圖得到它的地方:

public class CustomAuthentication implements AuthenticationProvider {

@Autowired
private AccountService accountService;

在我的另一堂課上,我也做同樣的事情,但是在這里行得通。

@Controller
public class AccountController {

@Autowired
private AccountService accountService;

當我從CustomAuthentication刪除這兩CustomAuthentication ,沒有任何錯誤。

配置以防萬一:

    <context:component-scan base-package="foo.bar" />

只有彈簧管理的對象才能自動裝配另一個組件/服務/存儲庫。

public class CustomAuthentication implements AuthenticationProvider需要進行spring管理

喜歡

@Controller
public class AccountController

嘗試使用@Component注釋CustomAuthenitcation

@Component
public class CustomAuthentication implements AuthenticationProvider

讓我知道如何創建CustomAuthentication Object。 CustomAuthentication對象應該是通過請求Spring(ApplicationContext.getBean()或在另一個bean中自動裝配)獲得的代理。

更新:

出現此錯誤的原因是您有兩個配置文件。 spring-servlet.xml和spring-security.xml。 在spring-security.xml中定義的Bean無法在其他Bean中找到。

所以你應該在spring-security.xml嘗試類似<import resource="spring-servlet.xml"/>

請嘗試<context:component-scan base-package="foo.bar.*" /><context:component-scan base-package="foo.bar.service" /> 我相信它會起作用。

你必須寫在AccountServiceImpl類@Service(“accountService”)

暫無
暫無

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

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