繁体   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