title: Context initialization failed(Caused by No qualifying bean of type)
body: <p>I tried to run my first web application using Spring MVC and Hibernate, but I have some problem.</p>
  
  <p>GenericDao:</p>
  
  <pre><code>@Repository
  public abstract class GenericDao&lt;T&gt; implements GeneralDao&lt;T&gt; {
  
      private Class&lt;T&gt; className;
  
      protected GenericDao(Class&lt;T&gt; className) {
          this.className = className;
      }
  
      @PersistenceContext
      private EntityManager entityManager;
  
      public EntityManager getEntityManager() {
          return entityManager;
      }
  
      @Override
      public void add(T object) {
          try {
              getEntityManager().persist(object);
          } catch (HibernateException e) {
              throw new DaoException(ErrorMessage.MESSAGE_ADD_ENTITY_FAIL, e);
          }
      }
  
      @Override
      public void update(T object) {
          try {
              getEntityManager().merge(object);
          } catch (HibernateException e) {
              throw new DaoException(ErrorMessage.MESSAGE_UPDATE_ENTITY_FAIL, e);
          }
      }
  
      @Override
      public void remove(T object) {
          try {
              getEntityManager().remove(object);
          } catch (HibernateException e) {
              throw new DaoException(ErrorMessage.MESSAGE_REMOVE_ENTITY_FAIL, e);
          }
      }
  
      @Override
      public T getById(int id) {
          try {
              return getEntityManager().find(this.className, id);
          } catch (HibernateException e) {
              throw new DaoException(ErrorMessage.MESSAGE_GET_BY_ID_ENTITY_FAIL, e);
          }
      }
  
      public abstract List&lt;T&gt; getAll() throws DaoException;
  
  }
  </code></pre>
  
  <p>GenericService:</p>
  
  <pre><code>@Service("genericService")
  public abstract class GenericService&lt;T&gt; implements GeneralService&lt;T&gt; {
  
      public abstract GenericDao&lt;T&gt; getGenericDao();
  
      @Transactional
      @Override
      public void add(T object) throws ServiceException {
          try {
              getGenericDao().add(object);
          } catch (DaoException e) {
              throw new ServiceException(e.getMessage());
          }
      }
  
      @Transactional
      @Override
      public void update(T object) throws ServiceException {
          try {
              getGenericDao().update(object);
          } catch (DaoException e) {
              throw new ServiceException(e.getMessage());
          }
      }
  
      @Transactional
      @Override
      public void remove(T object) throws ServiceException {
          try {
              getGenericDao().remove(object);
          } catch (DaoException e) {
              throw new ServiceException(e.getMessage());
          }
      }
  
      @Transactional(readOnly = true)
      @Override
      public T getById(int id) throws ServiceException {
          try {
              return getGenericDao().getById(id);
          } catch (DaoException e) {
              throw new ServiceException(e.getMessage());
          }
      }
  
      @Transactional(readOnly = true)
      @Override
      public List&lt;T&gt; getAll() throws ServiceException {
          try {
              return getGenericDao().getAll();
          } catch (DaoException e) {
              throw new ServiceException(e.getMessage());
          }
      }
  
  }
  </code></pre>
  
  <p>UserDao.java</p>
  
  <pre><code>@Repository
  public class UserDao extends GenericDao&lt;User&gt; {
      private final static String USER_LOGIN = "login";
      private final static String USER_PASSWORD = "password";
  
      @Autowired
      private UserDao() {
          super(User.class);
      }
  
      @Override
      public List&lt;User&gt; getAll() {
          List&lt;User&gt; userList;
          try {
              userList = getEntityManager().createQuery(Statement.GET_ALL_USERS).getResultList();
          } catch (HibernateException e) {
              throw new DaoException(ErrorMessage.MESSAGE_GET_ALL_ENTITY_FAIL, e);
          }
          return userList;
      }
  
      @Transactional
      public List&lt;User&gt; getByLoginAndPassword(String userLogin, String userPassword) {
          CriteriaQuery&lt;User&gt; criteriaQuery;
          try {
              CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
              criteriaQuery = criteriaBuilder.createQuery(User.class);
              Root&lt;User&gt; userRoot = criteriaQuery.from(User.class);
              criteriaQuery.select(userRoot);
              criteriaQuery.where(
                      criteriaBuilder.equal(userRoot.get(USER_LOGIN), userLogin),
                      criteriaBuilder.equal(userRoot.get(USER_PASSWORD), userPassword)
              );
          } catch (HibernateException e) {
              throw new DaoException(ErrorMessage.MESSAGE_GET_ENTITY_BY_LOGIN_AND_PASSWORD_FAIL, e);
          }
          return getEntityManager().createQuery(criteriaQuery).getResultList();
      }
  }
  </code></pre>
  
  <p>UserService.java</p>
  
  <pre><code>@Service("userService")
  @Transactional
  public class UserService extends GenericService&lt;User&gt; {
  
      private static Logger logger = Logger.getLogger(UserService.class);
  
      @Autowired
      private UserDao userDao;
  
      @Override
      public GenericDao&lt;User&gt; getGenericDao() {
          return userDao;
      }
  
      public String checkUser(String userLogin, String userPassword) throws ServiceException {
          String namePage = "errorAuthorization";
          List&lt;User&gt; userList;
          try {
             userList = userDao.getByLoginAndPassword(userLogin, userPassword);
          }  catch (DaoException e) {
              logger.debug(e);
              throw new ServiceException(e.getMessage());
          }
          if(userList.size() != 0) {
              return UserRoleChecker.defineUserPage(userList.get(0));
          }
          return namePage;
      }
  
      public void addUser(String userLogin, String userPassword, String userMail) throws ServiceException{
          Role role = new Role(0, RoleType.USER);
          User user = new User(0, userLogin, userPassword, userMail, role);
          add(user);
      }
  
  }
  </code></pre>
  
  <p>root-context.xml:</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:p="http://www.springframework.org/schema/p"
         xmlns:c="http://www.springframework.org/schema/c"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:util="http://www.springframework.org/schema/util"
         xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
         xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
          http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"&gt;
  
      &lt;context:annotation-config /&gt;
  
      &lt;context:component-scan base-package="by.netcracker.artemyev" /&gt;
  
      &lt;bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"&gt;
          &lt;property name="url" value="jdbc:mysql://localhost:3306/airline?useSSL=false" /&gt;
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver"/&gt;
          &lt;property name="username" value="root"/&gt;
          &lt;property name="password" value="root"/&gt;
          &lt;property name="initialSize" value="5"/&gt;
          &lt;property name="maxTotal" value="10"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="entityManager" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="packagesToScan" value="by.netcracker.artemyev" /&gt;
          &lt;property name="jpaVendorAdapter"&gt;
              &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
                  &lt;property name="showSql" value="true" /&gt;
                  &lt;property name="database" value="MYSQL" /&gt;
                  &lt;property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" /&gt;
              &lt;/bean&gt;
          &lt;/property&gt;
          &lt;property name="jpaProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
                  &lt;prop key="debug"&gt;true&lt;/prop&gt;
                  &lt;prop key="connection.isolation"&gt;2&lt;/prop&gt;
                  &lt;prop key="hibernate.cglib.use_reflection_optimizer"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
          &lt;property name="jpaDialect" ref="jpaDialect" /&gt;
      &lt;/bean&gt;
  
      &lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt;
  
      &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
      &lt;bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" /&gt;
  
      &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="entityManager" /&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="jpaDialect" ref="jpaDialect" /&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>Logs:</p>
  
  <pre><code>org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'userDao'; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'userDao' is expected to be of type 'by.netcracker.artemyev.dao.UserDao' but was actually of type 'com.sun.proxy.$Proxy31'
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
      at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
      at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:443)
      at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:325)
      at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
      at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4744)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5206)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
      at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752)
      at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728)
      at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
      at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1702)
      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:498)
      at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
      at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
      at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
      at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:456)
      at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:405)
      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:498)
      at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
      at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
      at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
      at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
      at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
      at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
      at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401)
      at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
      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:498)
      at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:324)
      at sun.rmi.transport.Transport$1.run(Transport.java:200)
      at sun.rmi.transport.Transport$1.run(Transport.java:197)
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
      at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
      at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
      at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:745)
  Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'userDao' is expected to be of type 'by.netcracker.artemyev.dao.UserDao' but was actually of type 'com.sun.proxy.$Proxy31'
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.checkBeanNotOfRequiredType(DefaultListableBeanFactory.java:1503)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1482)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
      ... 60 more
  </code></pre>
  
  <p>Questions:
  Please explain me why I have this problem? 
  How to fix this error?</p>