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>
servlet-context.xml 包含:
<context:component-scan base-package="com.kb.*" />
spring-security.xml 包含:
<!-- 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 { ...
customAuthenticationProvider
是用 @Component
注解的,为什么 spring 检测不到它?我收到错误:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'customAuthenticationProvider'
xml配置有错误,可能是什么原因?
最佳答案
简短回答: 使用正确的基础包名称。
更长的答案:
这不是基本包名称:com.kb.*
。
这是一个包名的梦想。
相反,请使用正确的基础包名称。例如,com.kb
更多答案:
身份验证提供程序中的 ref="customAuthenticationProvider"
属性似乎是问题所在。
具体来说,没有引用 ID 为 customAuthenticationProvider
的 bean。
尝试在 CustomAuthenticationProvider 类上使用 @Qualifier
注释。
关于java - Spring MVC 在使用 Component 注释时未检测到 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40749332/