title: Spring JavaConfig JPA issue - " No bean named 'entityManagerFactory' is defined"
body: <p>I'm in the process of converting XML based configuration for a Spring Java Web project to JavaConfig.</p>
  
  <p>I'm using JPA. So I created an additional JavaConfig class with the necessary configuration. But the JPA configuration fails for some reason.</p>
  
  <p>Here is my [web.xml][1]</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt;
  
       &lt;context-param&gt;
        &lt;param-name&gt;contextClass&lt;/param-name&gt;
        &lt;param-value&gt;
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        &lt;/param-value&gt;
    &lt;/context-param&gt;
  
    &lt;!-- Configuration locations must consist of one or more comma- or space-delimited
         fully-qualified @Configuration classes. Fully-qualified packages may also be
         specified for component-scanning --&gt;
  &lt;!--  &lt;context-param&gt;
        &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
        &lt;param-value&gt;com.my.contoso.spring.contosoJPA&lt;/param-value&gt;
    &lt;/context-param&gt;--&gt;
  
    &lt;!-- Bootstrap the root application context as usual using ContextLoaderListener --&gt;
    &lt;listener&gt;
        &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
    &lt;/listener&gt;
  
    &lt;!-- Declare a Spring MVC DispatcherServlet as usual --&gt;
    &lt;servlet&gt;
        &lt;servlet-name&gt;restservices&lt;/servlet-name&gt;
        &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
        &lt;!-- Configure DispatcherServlet to use AnnotationConfigWebApplicationContext
             instead of the default XmlWebApplicationContext --&gt;
        &lt;init-param&gt;
            &lt;param-name&gt;contextClass&lt;/param-name&gt;
            &lt;param-value&gt;
                org.springframework.web.context.support.AnnotationConfigWebApplicationContext
            &lt;/param-value&gt;
        &lt;/init-param&gt;
        &lt;!-- Again, config locations must consist of one or more comma- or space-delimited
             and fully-qualified @Configuration classes --&gt;
        &lt;init-param&gt;
            &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
            &lt;param-value&gt;com.my.contoso.spring.contosoConfig&lt;/param-value&gt;
        &lt;/init-param&gt;
    &lt;/servlet&gt;
  
      &lt;!--
          This Servlet mapping means that this Servlet will handle all incoming requests
       --&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;restservices&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
  
      &lt;!-- for CORS --&gt;
      &lt;filter&gt;
          &lt;filter-name&gt;CORS&lt;/filter-name&gt;
          &lt;filter-class&gt;com.thetransactioncompany.cors.CORSFilter&lt;/filter-class&gt;
  
          &lt;init-param&gt;
              &lt;param-name&gt;cors.supportedMethods&lt;/param-name&gt;
              &lt;param-value&gt;GET, HEAD, POST, PUT, DELETE, OPTIONS&lt;/param-value&gt;
          &lt;/init-param&gt;
  
          &lt;init-param&gt;
              &lt;param-name&gt;cors.supportedHeaders&lt;/param-name&gt;
              &lt;param-value&gt;Content-Type, X-Requested-With, Origin, Accept&lt;/param-value&gt;
          &lt;/init-param&gt;
  
      &lt;/filter&gt;
  
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;CORS&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
      &lt;!-- End for CORS --&gt;
  
      &lt;context-param&gt;
          &lt;param-name&gt;log4jConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;classpath:log4j.properties&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.util.Log4jConfigListener&lt;/listener-class&gt;
      &lt;/listener&gt;
  &lt;filter&gt;
          &lt;filter-name&gt;OpenEntityManagerFilter&lt;/filter-name&gt;
          &lt;filter-class&gt;org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter&lt;/filter-class&gt;
          &lt;init-param&gt;
          &lt;param-name&gt;entityManagerFactoryBeanName&lt;/param-name&gt;
           &lt;param-value&gt;entityManagerFactory&lt;/param-value&gt;
           &lt;/init-param&gt; 
  &lt;/filter&gt;
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;OpenEntityManagerFilter&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  &lt;!--    &lt;resource-ref&gt;
          &lt;description&gt;DB Connection&lt;/description&gt;
          &lt;res-ref-name&gt;jdbc/contosoDS&lt;/res-ref-name&gt;
          &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;
          &lt;res-auth&gt;Container&lt;/res-auth&gt;
      &lt;/resource-ref&gt;--&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>Here is my primary [JavaConfig class][2]</p>
  
  <pre><code>@Configuration
  @EnableWebMvc
  //@ImportResource("contoso-jpa.xml") //Import any existing xml context files
  @Import(contosoJPA.class)
  //@EnableTransactionManagement //substitute for  &lt;tx:annotation-driven/&gt;
  @ComponentScan({"com.my.contoso.webservices.rest", "com.my.contoso.services,com.my.contoso.dao"})
  public class contosoConfig {
  
  }
  </code></pre>
  
  <p>And Here's the JavaConfig class with [JPA config][3]</p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  @EnableJpaRepositories //(basePackages = { "com.my.contoso.domain" })
  public class contosoJPA {
  
  
           @Bean
          public DataSource dataSource() {
      //        return new EmbeddedDatabaseBuilder().setType(H2).build();
  
              DriverManagerDataSource ds = new DriverManagerDataSource();
  
  
          try {
              ds.setDriverClassName("com.mysql.jdbc.Driver");
              ds.setUsername("root");
              ds.setPassword("");
              ds.setUrl("jdbc:mysql://localhost:3306/contosodb");
  
          } catch (Exception e) {
      //        logger.error(e.getMessage());
              System.out.println(e);
          }
          return ds;
          }
  
          @Bean
          public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
              LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
              lef.setDataSource(dataSource);
              lef.setJpaVendorAdapter(jpaVendorAdapter);
              lef.setPackagesToScan("com.my.contoso.domain");
              lef.setPersistenceUnitName("contosoPU");
              return lef;
          }
  
          @Bean
          public JpaVendorAdapter jpaVendorAdapter() {
              HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
              hibernateJpaVendorAdapter.setShowSql(false);
              hibernateJpaVendorAdapter.setGenerateDdl(true);
              hibernateJpaVendorAdapter.setDatabase(Database.MYSQL);
      //        hibernateJpaVendorAdapter.setDatabase(Database.H2);
              return hibernateJpaVendorAdapter;
          }
  
          @Bean
          public PlatformTransactionManager transactionManager() {
              return new JpaTransactionManager();
          }   
  
  }
  </code></pre>
  
  <p>Full Stack Trace:</p>
  
  <pre><code>org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:570)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1108)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:278)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
      at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1121)
      at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.lookupEntityManagerFactory(OpenEntityManagerInViewFilter.java:222)
      at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.lookupEntityManagerFactory(OpenEntityManagerInViewFilter.java:205)
      at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:152)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at com.thetransactioncompany.cors.CORSFilter.doFilter(CORSFilter.java:179)
      at com.thetransactioncompany.cors.CORSFilter.doFilter(CORSFilter.java:241)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
      at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2430)
      at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2419)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
      at java.lang.Thread.run(Thread.java:744)
      org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:570)
  </code></pre>
  
  <p>'entityManagerFactory' is declared in the [web.xml][1]. The error says that a bean with such name doesn't exist. But I did include it in the  [JPA config][3] file. So I'm guessing that the beans in the JPA javaConfig file are not getting picked up. Wondering where the configuration is failing.</p>