I am trying to create a bean and than trying to inject the same in my Controller but i am getting bean creation failure error.Here is my code
@Service("springSecurityLoginServiceImpl")
public class SpringSecurityLoginServiceImpl implements SpringSecurityLoginService
{
//impl
}
this is how i am trying to inject it in my controller
@Controller
@RequestMapping("springSecurity/login.json")
public class SpringSecurityLoginController
{
@Autowired
@Qualifier("springSecurityLoginServiceImpl")
SpringSecurityLoginService springSecurityLoginService;
}
There is no entry in Spring-MVC-config xml file except these annotation, but when i am starting server facing the following exception
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)}
i am not sure what i am doing wrong or what extra i have to do
SpringSecurityLoginControllerclass refersSpringSecurityLoginServiceclass, for which a bean isn’t defined. That much the error says.It is true, because you’ve only defined a bean for the class
LoginServiceImpl, which doesn’t seem to extendSpringSecurityLoginServicein any way.Spring’s bean lookup algorithm first searches for beans of which type is, or extends,
SpringSecurityLoginService. Then, it narrows the avaialble options using theQualifier. In this case, no bean is found in the first place…See Spring doc:
You need that
LoginServiceImplwill implementSpringSecurityLoginService, for instance.EDIT
Since it was just a typo you might be not including
SpringSecurityLoginService‘s package incomponent-scantag, in your spring configuration file (as gkamal has already mentioned). You should have there something like:where
org.exampleshould be replaced bySpringSecurityLoginService‘s package.