java Spring 没有匹配的 bean 类型,预计至少有 1 个 bean

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13816558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:04:21  来源:igfitidea点击:

Spring No matching bean of type, expected at least 1 bean

javaspringspring-mvcautowired

提问by Jaanus

Getting error

获取错误

 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)}

my service:

我的服务:

public interface AccountService {

@Service
public class AccountServiceImpl implements AccountService {

So I should get the implementation

所以我应该得到实现

Where I am trying to get it :

我想在哪里得到它:

public class CustomAuthentication implements AuthenticationProvider {

@Autowired
private AccountService accountService;

In my other class also doing same thing, but there it works.

在我的其他班级也做同样的事情,但在那里它有效。

@Controller
public class AccountController {

@Autowired
private AccountService accountService;

When I remove the those 2 lines from my CustomAuthentication, getting no errors.

当我从我的CustomAuthentication. 中删除那两行时,没有错误。

configuration just incase:

配置只是柜面:

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

回答by Subin Sebastian

Only a spring managed object can Autowire another component/service/repository.

只有 spring 管理的对象可以自动装配另一个组件/服务/存储库。

public class CustomAuthentication implements AuthenticationProviderneed to be spring managed

public class CustomAuthentication implements AuthenticationProvider需要春季管理

like

喜欢

@Controller
public class AccountController

Try annotating the CustomAuthenitcation with @Component

尝试使用 @Component 注释 CustomAuthentication

@Component
public class CustomAuthentication implements AuthenticationProvider

and let me know how CustomAuthentication Object is created. CustomAuthentication object should be a proxy obtained by requesting to Spring(ApplicationContext.getBean() or autowired in another bean).

并让我知道 CustomAuthentication 对象是如何创建的。CustomAuthentication 对象应该是通过请求 Spring(ApplicationContext.getBean() 或在另一个 bean 中自动装配的) 获得的代理。

UPDATE:

更新:

reason for this error is you have two config files. spring-servlet.xml and spring-security.xml. Beans defined in spring-security.xml is not able to find those in other.

此错误的原因是您有两个配置文件。spring-servlet.xml 和 spring-security.xml。spring-security.xml 中定义的 Bean 无法在其他中找到。

so you should try something like <import resource="spring-servlet.xml"/>in spring-security.xml.

所以你应该尝试像<import resource="spring-servlet.xml"/>spring-security.xml.

回答by Sumit Desai

Please try <context:component-scan base-package="foo.bar.*" />or <context:component-scan base-package="foo.bar.service" />. I believe it would work.

请尝试<context:component-scan base-package="foo.bar.*" /><context:component-scan base-package="foo.bar.service" />。我相信它会起作用。

回答by user3029645

you have to write on AccountServiceImpl class @Service("accountService")

你必须在 AccountServiceImpl 类 @Service("accountService") 上写