title: Error creating bean with name 'initDbService': Injection of autowired dependencies failed
body: <p>How to remove this exception :(</p>
  
  <p>and suggest to tutorial provide, that show each and every step of lecture for beginners because it's don't know what will be next</p>
  
  <p><strong>UserService.java</strong></p>
  
  <pre><code>    @Service
      public class UserService {
  
          @Autowired
          private UserRepository userRepository;
  
          public List&lt;User&gt; findAll(){
              return userRepository.findAll();
          }
      }
  </code></pre>
  
  <p><strong>usercontroller.java</strong></p>
  
  <pre><code>@Controller
  public class UserController {
  
      @Autowired
      private UserService userService;
  
      @RequestMapping("/users")
      public String users(Model model){
          model.addAttribute("users", userService);
          return "users";
      }
  }
  </code></pre>
  
  <p><strong>InitDbService.java</strong></p>
  
  <pre><code>    @Transactional
      @Service
      public class InitDbService {
  
          @Autowired
          private UserRepository userRepository;
  
          @Autowired
          private RoleRepository roleRepository;
  
          @PostConstruct
          public void init()
          {
              Role roleUser=new Role();
              roleUser.setName("ROLE_USER");
              roleRepository.save(roleUser);
  
              Role roleAdmin=new Role();
              roleAdmin.setName("ROLE_ADMIN");
              roleRepository.save(roleAdmin);
  
              User userAdmin= new User();
              userAdmin.setName("admin");
              List&lt;Role&gt; roles=new ArrayList&lt;Role&gt;();
              roles.add(roleAdmin);
              roles.add(roleUser);
              userAdmin.setRoles(roles);
              userRepository.save(userAdmin);
  
          }
  
      }
  </code></pre>
  
  <p><strong>applicationContext.xml</strong></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:tx="http://www.springframework.org/schema/tx"
      xmlns:jpa="http://www.springframework.org/schema/data/jpa"
      xmlns:repository="http://www.springframework.org/schema/data/repository"
      xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd
          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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
          http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.7.xsd"&gt;
  
      &lt;context:component-scan base-package="mr.faizanabbas.mcs"&gt;
      &lt;context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/&gt;
      &lt;/context:component-scan&gt;
  
      &lt;jdbc:embedded-database type="HSQL" id="dataSource" /&gt;
  
      &lt;bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="emf"&gt;
      &lt;property name="packagesToScan" value="mr.faizanabbas.mcs.entity" /&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="jpaProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.hbm2ddl.auto"&gt;create&lt;/prop&gt;
              &lt;/props&gt;
      &lt;/property&gt;
      &lt;property name="persistenceProvider"&gt;
          &lt;bean class="org.hibernate.jpa.HibernatePersistenceProvider" /&gt;
      &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
      &lt;bean class="org.springframework.orm.jpa.JpaTransactionManager" id="TransactionManager"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;/bean&gt;
  
      &lt;jpa:repositories base-package="mr.faizanabbas.mcs.repository" /&gt;
  </code></pre>
  
  <p><strong>Exception:</strong></p>
  
  <pre><code>    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'initDbService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private mr.faizanabbas.mcs.repository.UserRepository mr.faizanabbas.mcs.service.InitDbService.userRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Cannot create inner bean '(inner bean)#1ba35ef' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1ba35ef': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined
          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
          at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
          at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
          at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
          at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
          at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:834)
          at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
          at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
          at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
          at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
          at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:798)
          at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:530)
          at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:771)
          at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:342)
          at org.eclipse.jetty.webapp.WebAppContext.startWebapp(WebAppContext.java:1368)
          at org.eclipse.jetty.maven.plugin.JettyWebAppContext.startWebapp(JettyWebAppContext.java:320)
          at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1335)
          at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:735)
          at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:259)
          at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:511)
          at org.eclipse.jetty.maven.plugin.JettyWebAppContext.doStart(JettyWebAppContext.java:403)
          at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
          at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
          at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
          at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
          at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:161)
          at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
          at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
          at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
          at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
          at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
          at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
          at org.eclipse.jetty.server.Server.start(Server.java:405)
          at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:106)
          at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
          at org.eclipse.jetty.server.Server.doStart(Server.java:372)
          at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
          at org.eclipse.jetty.maven.plugin.AbstractJettyMojo.startJetty(AbstractJettyMojo.java:457)
          at org.eclipse.jetty.maven.plugin.AbstractJettyMojo.execute(AbstractJettyMojo.java:328)
          at org.eclipse.jetty.maven.plugin.JettyRunMojo.execute(JettyRunMojo.java:170)
          at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
          at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
          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:116)
          at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
          at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
          at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
          at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
          at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
          at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
          at org.apache.maven.cli.MavenCli.execute(MavenCli.java:862)
          at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:286)
          at org.apache.maven.cli.MavenCli.main(MavenCli.java:197)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
          at java.lang.reflect.Method.invoke(Method.java:497)
          at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
          at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
          at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
          at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
      Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private mr.faizanabbas.mcs.repository.UserRepository mr.faizanabbas.mcs.service.InitDbService.userRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Cannot create inner bean '(inner bean)#1ba35ef' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1ba35ef': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined
          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:571)
          at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
          ... 65 more
      Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Cannot create inner bean '(inner bean)#1ba35ef' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1ba35ef': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 
  
      'entityManagerFactory' is defined
          at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:313)
          at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:129)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1481)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
          at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
          at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
          at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
          at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1145)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1069)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:967)
          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:543)
          ... 67 more
      Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1ba35ef': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined
          at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
          at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
          at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:634)
          at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:444)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
          at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:299)
          ... 80 more
      Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:698)
          at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1174)
          at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:283)
          at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
          at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
  </code></pre>