在Spring中实例化泛型类bean的问题 [英] Problem instantiating generic class bean in Spring
问题描述
我试图在Spring中实例化一个泛型类,但是我得到以下异常:
bean的初始化失败;嵌套的异常是
org.springframework.aop.framework.AopConfigException:无法生成类[class football.dao.jpa.GenericJpaDAO]的CGLIB子类:此问题的常见原因包括使用最终类或不可见类;嵌套异常是java.lang.IllegalArgumentException:超类没有null构造函数,但没有给出参数:
这是Spring的XML配置容器:
< bean id =clubDAOclass =football.dao.jpa.GenericJpaDAO>
< constructor-arg type =EntityManagerFactoryref =entityManagerFactory/>
< constructor-arg type =Classvalue =football.model.entities.ClubEntity/>
< constructor-arg type =Stringvalue =ClubEntity/>
< / bean>
这是泛型类:
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);
}
//更多方法
}
我不确定可能会造成这种情况。我会很感激任何想法。
这个问题与泛型无关,它是Spring AOP的限制。
如果使用CGLIB代理将方面(包括 @Transactional
方面)应用于类(如果目标类未实现任何接口或如果AOP配置为 proxy-target-class =true
),则不需要参数构造函数:
public class GenericJpaDAO< T extends HavingID> {
...
public GenericJpaDAO(){}
$ b $ public GenericJpaDAO(Class entityClass,String entityName,
EntityManagerFactory emFactory){... }
...
}
另请参阅: strong> I'm trying to instantiate a generic class in Spring, but I get following exception: Initialization of bean failed; nested exception is
org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class football.dao.jpa.GenericJpaDAO]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given: This is the XML configuration for Spring container: This is the generic class: I'm not really sure what could be causing this. I would appreciate any ideas. This problem is not related to generics, it's a limitation of Spring AOP. If aspects (including See also: 这篇关于在Spring中实例化泛型类bean的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
<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>
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
}
@Transactional
aspect) are applied to the class using CGLIB proxy (this happens if target class doesn't implement any interfaces or if AOP is configured with proxy-target-class = "true"
), no-argument constructor is required:public class GenericJpaDAO <T extends HavingID> {
...
public GenericJpaDAO() {}
public GenericJpaDAO( Class entityClass, String entityName,
EntityManagerFactory emFactory ) { ... }
...
}