Java SpringMVC在使用组件进行注释时不会检测到bean

Java SpringMVC在使用组件进行注释时不会检测到bean,java,spring,spring-mvc,Java,Spring,Spring Mvc,web.xml文件: <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/root-context.xml, /WEB-INF/spring-security.xml</param-value> </context-param> <listene

web.xml文件:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/root-context.xml, /WEB-INF/spring-security.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
customAuthenticationProvider
@Component
注释,所以为什么spring无法检测到它?我得到一个错误:

原因: org.springframework.beans.factory.noSuchBean定义异常:否 名为“customAuthenticationProvider”的bean


xml配置中有一些错误,可能是什么?

如果您使用的是xml配置,那么您需要将
以及添加到配置xml中,以便spring容器从注释中定位bean,您可以引用,或者需要从xml本身提供bean引用(通过取消注释


此外,正如DwB所建议的,除了此更改之外,您还需要将组件扫描更改为

,如果您使用的是xml配置,那么您需要将
以及
添加到您的配置xml中,以便spring容器从注释中定位bean,您可以参考,或者需要提供其他信息来自xml本身的bean引用(通过取消注释

此外,根据DwB的建议,除了此更改之外,您还需要将组件扫描更改为

简短回答: 使用正确的基本包名称

详细回答: 这不是基本包名:
com.kb.*
。 这是一个包名的梦想

相反,请使用正确的基本包名称。例如,
com.kb

更多答案: 身份验证提供程序中的
ref=“customAuthenticationProvider”
属性似乎是问题所在。 具体来说,没有引用id为
customAuthenticationProvider
的bean

尝试在CustomAuthenticationProvider类上使用限定符注释。

简短回答: 使用正确的基本包名称

详细回答: 这不是基本包名:
com.kb.*
。 这是一个包名的梦想

相反,请使用正确的基本包名称。例如,
com.kb

更多答案: 身份验证提供程序中的
ref=“customAuthenticationProvider”
属性似乎是问题所在。 具体来说,没有引用id为
customAuthenticationProvider
的bean


尝试在CustomAuthenticationProvider类上使用
@Qualifier
注释。

我按照您的说法将其添加到servlet-context.xml中。但是spring-suqurity.xml仍然显示警告
未找到引用的bean“CustomAuthenticationProvider”,并且我得到了相同的错误。您需要按照您在servl-context.xml中的说法将其添加到这两个内容中et-context.xml。但是spring-suqurity.xml仍然显示警告
未找到引用的bean“customAuthenticationProvider”
,并且我得到了相同的错误。您需要两个基本包名称的内容,错误相同。两个基本包名称的错误相同。
<context:component-scan base-package="com.kb.*" />
<!-- if uncoment the below line then customAuthenticationProvider is detected -->
<!--    <beans:bean id="customAuthenticationProvider" class="com.kb.authentication.provider.CustomAuthenticationProvider" />
 -->
    <authentication-manager erase-credentials="true">
        <authentication-provider ref="customAuthenticationProvider" />
    </authentication-manager>
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider { ...