@second_pilot

Spring Beans Generics?

В проекте есть вот такое определение ДАО.
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/>
}



И на него как-то плохо реагирует Spring


А вот файлик 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/>



При попытке получить объект типа ChatSessionDaoImpl посредством @Autowired вылетает вот что:

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



Сдается мне это из-за генериков и поведении Spring DI при рантайме. Кто-то сталкивался? Лечил? Я видел способ только отнаследовать от нужного класса, и не упоминать в потомке генериков, но не хочется делать это для каждого сервиса или дао.
  • Вопрос задан
  • 3434 просмотра
Решения вопроса 1
alexeygrigorev
@alexeygrigorev
Переворачиватель пингвинов
Сделайте для вашего класса HibernateCrudDao конструктор, который принимает класс. А в ChatSessionDaoImpl конструторе используйте что-то типа super(ChatSessionDaoImpl.class);

Или вот такое решение еще существует:
@SuppressWarnings(«unchecked»)
private Class getType() {
return (Class) ((ParameterizedType) getClass()
.getGenericSuperclass()).getActualTypeArguments()[0];
}

Можно в обобщенном классе использовать для получения параметра-генерика.
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы