pra*_*pes 8 generics spring dependency-injection
我试图在Spring中实例化一个泛型类,但我得到以下异常:
bean的初始化失败; 嵌套异常是org.springframework.aop.framework.AopConfigException:无法生成类[class football.dao.jpa.GenericJpaDAO]的CGLIB子类:此问题的常见原因包括使用final类或不可见类; 嵌套异常是java.lang.IllegalArgumentException:Superclass没有null构造函数但没有给出参数:
这是Spring容器的XML配置:
<bean id="clubDAO" class="football.dao.jpa.GenericJpaDAO">
<constructor-arg type="EntityManagerFactory" ref="entityManagerFactory"/>
<constructor-arg type="Class" value="football.model.entities.ClubEntity"/>
<constructor-arg type="String" value="ClubEntity"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
这是通用类:
public class GenericJpaDAO <T extends HavingID> {
private EntityManager em;
private Class entityClass;
private String entityName;
public GenericJpaDAO( Class entityClass, String entityName,
EntityManagerFactory emFactory ) {
this.entityClass = entityClass;
this.entityName = entityName;
em = emFactory.createEntityManager();
}
@Transactional
public void create( T entity ) {
em.persist( entity );
}
// more methods
}
Run Code Online (Sandbox Code Playgroud)
我不确定是什么导致了这一点.我很感激任何想法.
axt*_*avt 19
这个问题与泛型有关,它是Spring AOP的限制.
如果@Transactional
使用CGLIB代理将方面(包括方面)应用于类(如果目标类未实现任何接口或者如果配置了AOP,则会发生这种情况proxy-target-class = "true"
),则需要无参数构造函数:
public class GenericJpaDAO <T extends HavingID> {
...
public GenericJpaDAO() {}
public GenericJpaDAO( Class entityClass, String entityName,
EntityManagerFactory emFactory ) { ... }
...
}
Run Code Online (Sandbox Code Playgroud)
也可以看看:
归档时间: |
|
查看次数: |
11031 次 |
最近记录: |