title: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined
body: <p>I using Spring + Hibernate + JPA and Tomcat 7 for REST service. When I start the app I get the following:</p>
  
  <pre><code> org.apache.catalina.core.ApplicationContext log org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mainController': Unsatisfied dependency expressed through field 'carService': Error creating bean with name 'carServiceImpl': Unsatisfied dependency expressed through field 'carDao': Error creating bean with name 'carDao': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carDao': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'carServiceImpl': Unsatisfied dependency expressed through field 'carDao': Error creating bean with name 'carDao': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carDao': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined
     Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'carServiceImpl': Unsatisfied dependency expressed through field 'carDao': Error creating bean with name 'carDao': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carDao': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined
     Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carDao': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined
  </code></pre>
  
  <p>Below i show my spring config class </p>
  
  <pre><code>@Configuration
  @Import(DispatcherServletConfig.class)
  @ComponentScan(basePackages = "org.parkingTracker.controller, org.parkingTracker.service, org.parkingTracker.dao")
  @ImportResource("classpath*:/dao/src/main/resources/spring/dao-context.xml")
  public class RootConfig {
      public RootConfig() {}
  }
  
  @Configuration
  @EnableWebMvc
  @ComponentScan(basePackages = "org.parkingTracker.controller, org.parkingTracker.service")
  public class DispatcherServletConfig {
      public DispatcherServletConfig() {}
  }
  </code></pre>
  
  <p>I also have service and dao classes for access to data. To inject EntityManager in my dao i am using <code>@PersistenceContext</code> annotation, for inject dao for service  class i am using simple spring annotations. Below you can see my spring xml config for DAO layout. <strong>One important comments, when i run test for dao classes all passing right, and i don't have any Exceptions, and i am obtaining valid data</strong> </p>
  
  <pre><code>&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:tx="http://www.springframework.org/schema/tx"
      xmlns:jpa = "http://www.springframework.org/schema/data/jpa"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.3.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
      http://www.springframework.org/schema/data/jpa
      http://www.springframework.org/schema/data/jpa/spring-jpa.xsd "&gt;
  &lt;context:component-scan base-package="org.parkingTracker.dao"/&gt;
  &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
      &lt;property name="entityManagerFactory" ref="emf"/&gt;
  &lt;/bean&gt;
  &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;
      &lt;property name="driverClassName" value="org.postgresql.Driver"/&gt;
      &lt;property name="url" value="jdbc:postgresql://localhost:5432/parking"/&gt;
      &lt;property name="username" value="postgres"/&gt;
      &lt;property name="password" value="a1f10g"/&gt;
      &lt;property name="initialSize" value="20"/&gt;
      &lt;property name="maxActive" value="100"/&gt;
  &lt;/bean&gt;
  &lt;bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource"/&gt;
      &lt;property name="jpaVendorAdapter"&gt;
          &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
              &lt;property name="showSql" value="false"/&gt;
          &lt;/bean&gt;
      &lt;/property&gt;
      &lt;property name="packagesToScan" value="org.parkingTracker.model"/&gt;
      &lt;property name="jpaProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.PostgreSQL94Dialect&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;
  &lt;jpa:repositories  base-package="org.parkingTracker"
                     entity-manager-factory-ref="emf"
                     transaction-manager-ref="transactionManager"/&gt;&lt;/beans&gt;
  </code></pre>
  
  <p>CarServiceImpl:</p>
  
  <pre><code> public class CarServiceImpl implements CarService {
  @Autowired
  private CarDao carDao;
  @Override
  public void saveCar(Car car) throws EntityAlreadyExistException {
      carDao.saveCar(car);
  }
  @Override
  public Car getCarById(int id) {
      return carDao.getCarById(id);
  }
  @Override
  public Car getCarByIdWithTimeSpend(int id) {
      return carDao.getCarByIdWithTimeSpend(id);
  }
  @Override
  public Car getCarByNumber(String number) {
      return carDao.getCarByNumber(number);
  }
  @Override
  public Car getCarByNumberWithTimeSpend(String number) {
      return carDao.getCarByNumberWithTimeSpend(number);
  }
  </code></pre>
  
  <p>CarDao:</p>
  
  <pre><code>@Transactional
  @Repository("carDao")
  public class CarDaoImp implements CarDao{
      private final String EXIST_SQL = "SELECT 1 FROM car WHERE car_num = :num";
      @PersistenceContext
      private EntityManager entityManager;
      @Transactional()
      public void saveCar(Car car) throws EntityAlreadyExistException {
          if(entityManager.createNativeQuery(EXIST_SQL).setParameter("num", car.getNumber()).getSingleResult()!=null){
              throw new  EntityAlreadyExistException();
          }else {
              entityManager.persist(car);
          }
      } 
      @Transactional(readOnly = true)
      public Car getCarById(int id) {
          return entityManager.find(Car.class, id);
      }
      @Transactional(readOnly = true)
      public Car getCarByIdWithTimeSpend(int id) {
          Car car = entityManager.find(Car.class, id);
          Hibernate.initialize(car.getTimeSet());
          return car;
      }
      @Override
      @Transactional(readOnly = true)
      public Car getCarByNumber(String number) throws NoResultException{
          CriteriaBuilder builder  = entityManager.getCriteriaBuilder();
          CriteriaQuery&lt;Car&gt; carCriteriaQuery = entityManager.getCriteriaBuilder().createQuery(Car.class);
          Root&lt;Car&gt; root = carCriteriaQuery.from(Car.class);
          carCriteriaQuery.select(root);
          carCriteriaQuery.where(builder.equal( root.get(Car_.number), number ));
          return entityManager.createQuery(carCriteriaQuery).getSingleResult();
      }
      @Override
      @Transactional(readOnly = true)
      public Car getCarByNumberWithTimeSpend(String number){
          CriteriaBuilder builder  = entityManager.getCriteriaBuilder();
          CriteriaQuery&lt;Car&gt; carCriteriaQuery = entityManager.getCriteriaBuilder().createQuery(Car.class);
          Root&lt;Car&gt; root = carCriteriaQuery.from(Car.class);
          carCriteriaQuery.select(root);
          carCriteriaQuery.where(builder.equal( root.get(Car_.number), number ));
          Car car = entityManager.createQuery(carCriteriaQuery).getSingleResult();
          Hibernate.initialize(car.getTimeSet());
          return car;
      }
  }
  </code></pre>
  
  <p>web.xml :</p>
  
  <pre><code>&lt;web-app
          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_3_0.xsd"
          version="3.0"&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;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;
              org.parkingTracker.controller.config.RootConfig
          &lt;/param-value&gt;
      &lt;/context-param&gt;    
      &lt;listener&gt;
          &lt;listener-class&gt;
              org.springframework.web.context.ContextLoaderListener
          &lt;/listener-class&gt;
      &lt;/listener&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;mvc-dispatcher&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;contextClass&lt;/param-name&gt;
              &lt;param-value&gt;                org.springframework.web.context.support.AnnotationConfigWebApplicationContext
              &lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
              &lt;param-value&gt;
                  org.parkingTracker.controller.config.DispatcherServletConfig
              &lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;mvc-dispatcher&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>