title: Spring 4.0: Resolve Type of GenericDao
body: <p>I updated my Spring MVC environment to Version 4.0.0.RELEASE. After reading this article (<a href="https://spring.io/blog/2013/12/03/spring-framework-4-0-and-java-generics" rel="nofollow">https://spring.io/blog/2013/12/03/spring-framework-4-0-and-java-generics</a>), I tried to use the new generics support, to autowire my GenericDAOs. </p>
  
  <p><strong>Spring 3.2</strong></p>
  
  <p>In Spring 3.2 environment I created for each DAO instance a separate bean and passed the class as paramameter to constructor. This because as I am aware, it is not possible to resolve a generic type at runtime.</p>
  
  <pre><code>@Bean
  public GenericDaoImpl&lt;Permission, Integer&gt; permissionDao() {
      return new GenericDaoImpl&lt;Employee, Integer&gt;(Employee.class, sessionFactory); 
  }
  </code></pre>
  
  <p><strong>Spring 4.0.0.RELEASE</strong></p>
  
  <p>With spring 4.0 I just have to autowire my GenericDAO in Service like:</p>
  
  <pre><code>@Autowired private GenericDaoImpl&lt;Employee, Integer&gt; employeeDao;
  </code></pre>
  
  <p>And within GenericDAOImpl:</p>
  
  <pre><code>@Repository
  public class GenericDaoImpl&lt;T, PK extends Serializable&gt; implements GenericDao&lt;T, Serializable&gt; {
  
          @Autowired
          private SessionFactory sessionFactory;
          private Class&lt;?&gt; genericType;
  </code></pre>
  
  <p>This solution is much easier and does work for most of my DAO methods. But for a few of them I need to know which type my GenericDAO instance has.</p>
  
  <pre><code>@Override
  public List&lt;T&gt; findAll() {
      return getCurrentSession().createQuery("FROM " + getGenericType().getName()).list();
  }
  </code></pre>
  
  <p>So I tried to resolve the type with the static methods of <code>GenericTypeResolver</code> and <code>ResolvableType</code>. But I am not able to resolve the type. Is this still not possible? </p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>UPDATE:</strong> </p>
  
  <pre><code>private Class&lt;T&gt; getGenericType() {
      return (Class&lt;T&gt;) GenericTypeResolver.resolveTypeArgument(GenericDaoImpl.class, GenericDao.class);
  }
  </code></pre>