Bean创建失败(spring)
我正在尝试创建一个bean,然后尝试在我的控制器中注入相同的bean,但是我得到了bean创建失败错误Bean创建失败(spring),spring,spring-mvc,Spring,Spring Mvc,我正在尝试创建一个bean,然后尝试在我的控制器中注入相同的bean,但是我得到了bean创建失败错误 @Service("springSecurityLoginServiceImpl") public class SpringSecurityLoginServiceImpl implements SpringSecurityLoginService { //impl } 这就是我试图将其注入控制器的方式 @Controller @RequestMapping("springSecurit
@Service("springSecurityLoginServiceImpl")
public class SpringSecurityLoginServiceImpl implements SpringSecurityLoginService
{
//impl
}
这就是我试图将其注入控制器的方式
@Controller
@RequestMapping("springSecurity/login.json")
public class SpringSecurityLoginController
{
@Autowired
@Qualifier("springSecurityLoginServiceImpl")
SpringSecurityLoginService springSecurityLoginService;
}
在SpringMVCConfigXML文件中,除了这些注释外,并没有任何条目,但当我启动服务器时,会遇到以下异常
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0'
defined in ServletContext resource [/WEB-INF/config/spring-mvc-config.xml]:
Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'springSecurityLoginController':
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: com.core.servicelayer.user.SpringSecurityLoginService com.storefront.controllers.pages.SpringSecurityLoginController.springSecurityLoginService;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No matching bean of type [com.core.servicelayer.user.SpringSecurityLoginService] 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),
@org.springframework.beans.factory.annotation.Qualifier(value=springSecurityLoginServiceImpl)}
我不确定我做错了什么,或者我必须做什么额外的事情
SpringSecurityLoginController
类指的是未定义bean的SpringSecurityLoginService
类。错误说明了这一点
这是真的,因为您只为类LoginServiceImpl
定义了一个bean,它似乎没有以任何方式扩展SpringSecurityLoginService
Spring的bean查找算法首先搜索类型为或扩展为SpringSecurityLoginService的bean。然后,它使用限定符缩小可用选项的范围。在这种情况下,首先没有找到bean
见:
4.11.3带限定符的基于注释的微调自动布线
由于按类型自动关联可能导致多个候选项,因此通常
有必要对选择过程进行更多控制。单程
这是通过Spring的@Qualifier注释实现的。这允许
用于将限定符值与特定参数关联,缩小
类型集匹配,以便为每个类型选择特定的bean
争论
例如,您需要LoginServiceImpl
将实现SpringSecurityLoginService
编辑
由于这只是一个输入错误,您可能没有在spring配置文件的组件扫描标记中包含SpringSecurityLoginService
的包(正如gkamal已经提到的)。你应该有这样的东西:
<context:component-scan base-package="org.example"/>
其中org.example
应该被SpringSecurityLoginService
的软件包所取代。我的错误,因为我刚刚在帖子中输入了一些错误。请查看更新的帖子作为SpringSecurityLoginService
由SpringSecurityLoginServiceImpl
实现的SpringSecurityLoginServiceImpl
可以显示您在中使用的组件扫描标签吗spring-mvc-config.xml?