Abstraktes DAO-Muster und Spring-Problem „Proxy kann nicht in ... umgewandelt werden!
Ich weiß, dass dies sehr oft gefragt wird, aber ich kann keine funktionierende Lösung finden:
Das ist mein AbstractDAO:
public interface AbstractDao<T>
{
public T get(Serializable id);
//other CRUD operations
}
Und dies ist die Implementierung meiner JPA:
public abstract class AbstractDaoJpaImpl<T> implements AbstractDao<T> , Serializable
{
protected EntityManager em;
protected Class<T> clazz;
@SuppressWarnings("unchecked")
public AbstractDaoJpaImpl()
{
ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
this.clazz = (Class<T>) genericSuperclass.getActualTypeArguments()[0];
}
public abstract void setEntityManager(EntityManager em);
//implementations skipped
}
Und dies ist das Dao einer Entität:
public interface PersonDao extends AbstractDao<Person>
{
//empty
}
Hier ist die Implementierung:
@Repository
public class PersonDaoImpl extends AbstractDaoJpaImpl<Person> implements PersonDao , OtherInterface
{
@PersistenceContext(unitName="company")
@Override
public void setEntityManager(EntityManager em)
{
this.em = em;
}
@Override // implements OtherInterface.additionalMethods()
public additionalMethods()
{
// implements...
}
}
Die gesamte Architektur ist einfach:
Interface AbstractDao definiert einfache CRUD-Methoden.
Interface PersonDao erweitert AbstractDAO ohne zusätzliche Methoden.
class AbstractDaoJpaImpl definiert JPAs Implementierung von AbstractDao
class PersonDaoImpl erweitert AbstractDaoJpaImpl und implementiert PersonDaoAND OtherInterface, das aditionalMethods () @ hinzufü ...
IF, PersonDaoImpl implementiert nur PersonDao, ohne OtherInterface.additionalMethods () zu implementieren.
Ich kann nutze
<tx:annotation-driven transaction-manager="transactionManager" />
in der XML-Datei meines Frühlings.
ABER, PersonDaoImpl implementiert OtherInterface (s), beim Testen / Laufen, Imüssen Sie den DAO von PersonDao in PersonDaoImpl oder OtherInterfaces umwandeln , sowie
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:app.xml"})
@TransactionConfiguration(transactionManager="transactionManager" , defaultRollback=false)
public class PersonDaoTest
{
@Inject
PersonDao dao;
@Test
public void testAdditionalMethod()
{
PersonDaoImpl impl = (PersonDaoImpl) dao;
System.out.println(impl.additionalMethod(...));
}
}
Das Problem tritt auf, wenn(PersonDaoImpl) dao
, wodurch die Ausnahme "Proxy kann nicht in PersonDaoImpl umgewandelt werden" ausgelöst wird:
java.lang.ClassCastException: $Proxy36 cannot be cast to foobar.PersonDaoImpl
at foobar.PersonDaoTest.testAdditionalMethod(PersonDaoTest.java:36)
dies wird oft beim googeln gefragt, jeder schlägt vor, @ hinzuzufügproxy-target-class="true"
zu<tx:annotation-driven>
:
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
Dies verwendet CGLIB anstelle des dynamischen Proxys von JDK.
ABER beim Initialisieren von Spring wird eine weitere Ausnahme ausgelöst:
Caused by: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
in AbstractDaoJpaImpls Konstruktor:
ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
Jede Frage hört hier auf, ich kann keine findenArbeite solutions now.
Kann mir jemand eine funktionierende Lösung geben? Vielen Dank
Umgebung: Spring-3.0.4, javaee-api-6.0, javax.inject, cglib-2.2, hibernate-jpa-2.0-api-1.0.0,