我有一个基于 Web 服务的应用程序,其中存在多个类。一些类类型是抽象的,在其他类中扩展以定义抽象类方法的主体。我使用 spring 来实例化 spring context xml 中的类。我使用了 context:component-scan base-package="com.test.webservices"
它扫描所有 @Component
类并实例化,但它不会扫描抽象类.
现在我的问题是有没有使用组件扫描扫描抽象类的解决方案?
代码就像-
@Component
public abstract class HibernateEntityManager{ ... }
@Component
public class HibernateGenericDaoImpl extends HibernateEntityManager{ ... }
在 applicationContext.xml 中我有
<context:component-scan base-package="com.test.webservices"/>
扫描除抽象类之外的所有类。
异常(exception)是:-
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManager' defined in ServletContext resource [/WEB-INF/classes/ApplicationContext/applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.test.webservices.utils.HibernateEntityManager]: Is it an abstract class?; nested exception is java.lang.InstantiationException
最佳答案
您的抽象类不能专门是一个 bean,因为您无法实例化它。从抽象类中删除注释并仅将其放在具体类上。
关于带有抽象类的 java.lang.InstantiationException 使用组件扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31043865/