title: NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined
body: <p>I have simple Spring 4 WebMVC app (Java-config), and I want to add JPA.
  But when I try to run app (as deloyed on Tomcat) I get:
  What can be a source of error?</p>
  
  <blockquote>
    <p>Error creating bean with name 'indexController': Injection of
    autowired dependencies failed; nested exception is
    org.springframework.beans.factory.BeanCreationException: Could not
    autowire field: org.demo.webtemplate.db.repository.CustSysRepository
    org.demo.webtemplate.controllers.IndexController.repository; nested
    exception is org.springframework.beans.factory.BeanCreationException:
    Error creating bean with name 'custSysRepository': Cannot create inner
    bean '(inner bean)#f4da8a0' 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)#f4da8a0': Cannot resolve
    reference to bean 'entityManagerFactory' while setting constructor
    argument; nested exception is
    org.springframework.beans.factory.<strong>NoSuchBeanDefinitionException: No
    bean named 'entityManagerFactory' is defined</strong></p>
  </blockquote>
  
  <p><strong>Initializer:</strong></p>
  
  <pre><code>package org.demo.webtemplate;
  
  ...
  
  public class SpringWebMvcInitializer implements WebApplicationInitializer  {
  
      @Override
      public void onStartup(ServletContext servletContext) throws ServletException {
          AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
          ctx.register(Config.class);
          ctx.setServletContext(servletContext);
          Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
          servlet.addMapping("/");
          servlet.setLoadOnStartup(1);
      }
  
  }
  </code></pre>
  
  <p><strong>Config:</strong></p>
  
  <pre><code>package org.demo.webtemplate;
  
  ...
  
  @Configuration
  @EnableWebMvc
  @ComponentScan("pl.bzwbk.webtemplate")
  @EnableJpaRepositories("pl.bzwbk.webtemplate.db.repository")
  public class Config {
  
      @Bean
      public UrlBasedViewResolver setupViewResolver() {
          UrlBasedViewResolver resolver = new UrlBasedViewResolver();
          resolver.setPrefix("/WEB-INF/views/");
          resolver.setSuffix(".jsp");
          resolver.setViewClass(JstlView.class);
          return resolver;
      }
  }
  </code></pre>
  
  <p><strong>Controller:</strong></p>
  
  <pre><code>package org.demo.webtemplate.controllers;
  
  ...
  
  @Controller
  public class IndexController {
  
      @Autowired
      CustSysRepository repository;
  
      @RequestMapping(value = "/", method = RequestMethod.GET)
      public String index() {
          List&lt;CustSys&gt; clients = repository.findByFullName("SOME NAME");
  
          return "index"+clients .size();
      }
  
  }
  </code></pre>
  
  <p><strong>Repository:</strong></p>
  
  <pre><code>package org.demo.webtemplate.db.repository;
  
  ...
  
  public interface CustSysRepository extends JpaRepository&lt;CustSys, Long&gt; {
  
      List&lt;CustSys&gt; findByFullName(String fullName);
  }
  </code></pre>
  
  <p><strong>Entity:</strong></p>
  
  <pre><code>package org.demo.webtemplate.db.entity;
  
  ...
  
  @Entity
  @Table(name = "CUST_SYS")
  public class CustSys implements Serializable {
      private static final long serialVersionUID = 1L;
      ...
      @Size(max = 255)
      @Column(name = "FULL_NAME")
      private String fullName;
      ...
      public String getFullName() {
          return fullName;
      }
  
      public void setFullName(String fullName) {
          this.fullName = fullName;
      }
      ...
  }
  </code></pre>
  
  <p><strong>application.properties:</strong></p>
  
  <pre><code>jdbc.driverClassName=org.hsqldb.jdbc.JDBCDriver
  jdbc.url=jdbc:hsqldb:mem:testdb
  jdbc.user=sa
  jdbc.pass=
  </code></pre>