title: Unable to Autowire ServiceImpl Class Bean with Service Class Object
body: <p>The error which I'm facing is as follows:</p>
  
  <pre><code>    root cause
  
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginAndSignUpController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.frosters.firststep.service.LoginAndSignUpService com.frosters.firststep.controller.LoginAndSignUpController.loginAndSignUpService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.frosters.firststep.service.LoginAndSignUpService] 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.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
      org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
      org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
      org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
      org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
      org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
      org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
      org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
      org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
      org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:834)
      org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
      org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:667)
      org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:633)
      org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:681)
      org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:552)
      org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:493)
      org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
      javax.servlet.GenericServlet.init(GenericServlet.java:158)
      org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
      org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
      org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
      org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
      org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
      org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
      org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
      java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      java.lang.Thread.run(Thread.java:745)
  root cause
  
  org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.frosters.firststep.service.LoginAndSignUpService com.frosters.firststep.controller.LoginAndSignUpController.loginAndSignUpService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.frosters.firststep.service.LoginAndSignUpService] 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.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:571)
      org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
      org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
      org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
      org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
      org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
      org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
      org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
      org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
      org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
      org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
      org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:834)
      org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
      org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:667)
      org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:633)
      org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:681)
      org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:552)
      org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:493)
      org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
      javax.servlet.GenericServlet.init(GenericServlet.java:158)
      org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
      org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
      org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
      org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
      org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
      org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
      org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
      java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      java.lang.Thread.run(Thread.java:745)
  root cause
  
  org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.frosters.firststep.service.LoginAndSignUpService] 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.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1326)
      org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1072)
      org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:967)
      org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:543)
      org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
      org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
      org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
      org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
      org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
      org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
      org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
      org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
      org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
      org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
      org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:834)
      org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
      org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:667)
      org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:633)
      org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:681)
      org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:552)
      org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:493)
      org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
      javax.servlet.GenericServlet.init(GenericServlet.java:158)
      org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
      org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
      org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
      org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
      org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
      org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
      org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
      java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      java.lang.Thread.run(Thread.java:745)
  </code></pre>
  
  <p>I've tried resolving it through using @Component,@Service annotations and has tried defining the bean in XML Configuration as explained in other StackOverflow links but I'm getting the same error. May be I'm not getting it right or I'm doing something wrong.</p>
  
  <p>ControllerClass is as follows:</p>
  
  <pre><code>package com.frosters.firststep.controller;
  
  @RequestMapping(value="/login.html")
  @Controller
  public class LoginAndSignUpController {
  
      @Autowired
      private LoginAndSignUpService loginAndSignUpService;
  
      @ModelAttribute("userVO")
      public UserVO createUserVOBean(){
          return new UserVO();
      }
  
      @RequestMapping(method=RequestMethod.GET)
      public ModelAndView getLoginForm(){
          System.out.println("inside getLoginForm() method");
          return new ModelAndView("Login");
      }
  
      @RequestMapping(method=RequestMethod.POST)
      public ModelAndView verifyLogin(@ModelAttribute("userVO")UserVO userVO){
          System.out.println("inside verifyLogin");
          boolean result = loginAndSignUpService.verifyUser(userVO);
          ModelAndView model;
          if(!result){
              model = new ModelAndView("Login");
              model.addObject("loginError", "invalid username/password");
          }
          else
              model = new ModelAndView("LoginSuccess"); //temporary view
          return model;
      }
  
  }
  </code></pre>
  
  <p>ServiceImplClass is as follows:</p>
  
  <pre><code>package com.frosters.firststep.serviceimpl;
  
  public class LoginAndSignUpServiceImpl implements LoginAndSignUpService{
  
      @Autowired
      private LoginAndSignUpDAO loginAndSignUpDAO;
  
      public boolean verifyUser(UserVO userVO) {
          boolean result = loginAndSignUpDAO.validateUser(userVO);
          if(result)
              return true;
          else
              return false;
      }
  
  }
  </code></pre>
  
  <p>DAOImpl Class is as follows:</p>
  
  <pre><code>package com.frosters.firststep.daoimpl;
  
  public class LoginAndSignUpDAOImpl extends NamedParameterJdbcDaoSupport implements LoginAndSignUpDAO{
  
      public boolean validateUser(UserVO userVO) {
          String userName = userVO.getUserName();
          String password = userVO.getPassword();
          SqlParameterSource paramSource = new MapSqlParameterSource("username", userName);
          try{
          String pass = this.getNamedParameterJdbcTemplate().queryForObject(FSUtils.LOGIN_INFO, paramSource, String.class);
          if(pass != null &amp;&amp; !pass.isEmpty()){
              if(pass.equals(password))
                  return true;
              else
                  return false;
          }
          else
              return false;
      }catch(EmptyResultDataAccessException ex){
          return false;
          }
      }
  
  
      }
  </code></pre>
  
  <p>web.xml file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns="http://java.sun.com/xml/ns/javaee" 
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" 
      version="3.0"&gt;
  
    &lt;display-name&gt;Login And SignUp&lt;/display-name&gt;
  
    &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;/WEB-INF/classes/applicationContext.xml&lt;/param-value&gt;
    &lt;/context-param&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;spring-dispatcher-servlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
              &lt;param-value&gt;/WEB-INF/classes/spring-dispatcher-servlet.xml&lt;/param-value&gt;
          &lt;/init-param&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;spring-dispatcher-servlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>spring-dispatcher-servlet.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:jdbc="http://www.springframework.org/schema/jdbc"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
          http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd
          http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"&gt;
  
          &lt;context:component-scan base-package="com.frosters.firststep" /&gt;
          &lt;mvc:annotation-driven /&gt;
  
          &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
              &lt;property name="prefix" value="/WEB-INF/views/" /&gt;
              &lt;property name="suffix" value=".jsp" /&gt;
          &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>applicationContext.xml where I've defined the beans:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:jdbc="http://www.springframework.org/schema/jdbc"
      xmlns:lang="http://www.springframework.org/schema/lang"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
          http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd
          http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.2.xsd
          http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"&gt;
  
          &lt;context:property-placeholder location="classpath:DbUtil.properties"/&gt;
  
          &lt;!-- &lt;context:component-scan base-package="com.frosters.firststep" /&gt;
          &lt;mvc:annotation-driven /&gt; --&gt;
  
          &lt;bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
              &lt;property name="driverClassName" value="${jdbc.mysql.driverClassName}" /&gt;
              &lt;property name="url" value="${jdbc.mysql.url}" /&gt;
              &lt;property name="username" value="${jdbc.mysql.username}" /&gt;
              &lt;property name="password" value="${jdbc.mysql.password}" /&gt;
          &lt;/bean&gt;
  
          &lt;bean id="loginAndSignUpDAO" class="com.frosters.firststep.daoimpl.LoginAndSignUpDAOImpl"&gt;
              &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;/bean&gt;
  
          &lt;bean id="loginAndSignUpService" class="com.frosters.firststep.serviceimpl.LoginAndSignUpServiceImpl" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>I've tried defining all the beans in spring-dispatcher-servlet.xml also but doing this also doesn't resolve the problem.</p>
  
  <p>someone can please help me to see where the fault? .... Thank you ...</p>