S
S
second_pilot2011-07-20 09:09:55
Java
second_pilot, 2011-07-20 09:09:55

Spring Beans Generics?

The project has this definition of DAO.

package ....dao.impl;<br/>
<br/>
@Repository<br/>
public class ChatSessionDaoImpl extends HibernateCrudDao&lt;ChatSession&lt;User&lt;PersonalInfo, ContactInfo, LoginInfo, BusinessInfo&gt;&gt;&gt; implements ChatSessionDao {<br/>
 <br/>
}

And somehow Spring reacts badly to it
. And here is the file root-context.xml
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br/>
&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;<br/>
 xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;<br/>
 xmlns:context=&quot;http://www.springframework.org/schema/context&quot;<br/>
 xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans <a href="http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">www.springframework.org/schema/beans/spring-beans-3.0.xsd</a><br/>
 <a href="http://www.springframework.org/schema/context">www.springframework.org/schema/context</a> <a href="http://www.springframework.org/schema/context/spring-context.xsd">www.springframework.org/schema/context/spring-context.xsd</a><br/>
 <br/>
 &quot;&gt;<br/>
 <br/>
 <br/>
&lt;context:annotation-config /&gt;<br/>
&lt;context:component-scan base-package=&quot;....dao&quot;/&gt;<br/>
&lt;context:component-scan base-package=&quot;....service&quot;/&gt;<br/>
&lt;context:component-scan base-package=&quot;...o.dao.impl&quot;/&gt;<br/>
&lt;context:component-scan base-package=&quot;....service.impl&quot;/&gt;<br/>
 <br/>

When trying to get an object of type ChatSessionDaoImpl using @Autowired, the following crashes:
Error creating bean with name 'chatSessionDaoImpl' defined in file [/.../dao/impl/ChatSessionDaoImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [....dao.impl.ChatSessionDaoImpl]: Constructor threw exception; nested exception is java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl cannot be cast to java.lang.Class

It seems to me that this is due to generics and the behavior of Spring DI at runtime. Someone faced? Treated? I saw a way to only inherit from the desired class, and not mention generics in the descendant, but I don’t want to do this for every service or dao.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Ololesha Ololoev, 2011-07-20
@second_pilot

Make a constructor for your HibernateCrudDao class that takes a class. And in the ChatSessionDaoImpl constructor use something like super(ChatSessionDaoImpl.class);
Or this solution still exists:
@SuppressWarnings("unchecked")
private Class getType() {
return (Class) ((ParameterizedType) getClass()
.getGenericSuperclass()).getActualTypeArguments()[0];
}
Can be used in a generic class to get a generic parameter.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question