title: Failed injection of autowired dependencies
body: <p>I am using Spring3.1.1 with hibernate4 to implement a JEE project.</p>
  
  <p>I'm receiving the following error on the controller</p>
  
  <pre><code>SEVERE: Exception sending context initialized event to listener instance of class          org.springframework.web.context.ContextLoaderListener
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'createUserController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.myproject.service.user.UserManager com.myproject.controller.user.CreateUserController.userManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.myproject.service.user.UserManager] 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)}
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
  at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
  at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
  at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
  at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
  at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
  at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4135)
  at org.apache.catalina.core.StandardContext.start(StandardContext.java:4630)
  at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
  at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)
  at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
  at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:445)
  at org.apache.catalina.startup.Embedded.start(Embedded.java:825)
  at org.codehaus.mojo.tomcat.AbstractRunMojo.startContainer(AbstractRunMojo.java:558)
  at org.codehaus.mojo.tomcat.AbstractRunMojo.execute(AbstractRunMojo.java:255)
  at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
  at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
  at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
  at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
  at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
  at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
  at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
  at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
  at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
  at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
  at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
  at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
  at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:601)
  at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
  at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
  at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
  at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
  Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.myproject.service.user.UserManager com.myproject.controller.user.CreateUserController.userManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.myproject.service.user.UserManager] 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)}
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506)
  at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
  ... 43 more
  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.myproject.service.user.UserManager] 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)}
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
  ... 45 more
  </code></pre>
  
  <p>My servlet.xml is </p>
  
  <pre><code>&lt;?xml  version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans" ...
  
  &lt;context:annotation-config /&gt;
  &lt;context:component-scan base-package="com.myproject.controller.user" /&gt;
  &lt;context:component-scan base-package="com.myproject.service.user"/&gt;
  
  ....
  
  &lt;bean id="userSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource"&gt;&lt;/property&gt;
      &lt;property name="configLocation"&gt;
          &lt;value&gt;classpath:hibernate.cfg.xml&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;${jdbc.dialect}&lt;/prop&gt;
              &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
      &lt;property name="packagesToScan" value="com.myproject.entity.user" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="userDAO" class="com.myproject.dao.user.UserDAOImpl"&gt;&lt;/bean&gt;
  &lt;bean id="userManager" class="com.myproject.service.user.UserManagerImpl"&gt;&lt;/bean&gt;
  &lt;tx:annotation-driven /&gt;
  &lt;bean id="transactionManager"
      class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="userSessionFactory"&gt;&lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p></p>
  
  <p>My UserManagerImpl.java class</p>
  
  <pre><code>@Service
  public class UserManagerImpl implements UserManager {
  
  @Autowired
  private UserDAO userDAO;
  }
  </code></pre>
  
  <p>And the Controller.java</p>
  
  <pre><code>@Controller
  public class CreateUserController {
  
  @Autowired
  private UserManager userManager;
  }
  </code></pre>
  
  <p>I have tried adding <code>@Configuration</code> and <code>@ComponentScan("com.myproject.service.user")</code> to the controller and i still received the same error.</p>
  
  <p>Does anyone see why the autowiring &amp; scanning aren't working please?</p>