简体   繁体   中英

java.lang.InstantiationException with abstract class Using Conponent-Scan

I have an application that is based on webservices in which multiple classes are present. Some classes types are abstract that is extended in other classes for defining the body to the abstract class methods. I used spring for instantiate the classes in spring context xml. I have used context:component-scan base-package="com.test.webservices" it scan all the @Component class and instantiated but it will not scan the abstract class.

Now My question here is that is there any solution for scanning abstract class using component-scan ?

Code is like-

@Component
public abstract class HibernateEntityManager{ ... }

@Component
public class HibernateGenericDaoImpl extends HibernateEntityManager{ ... }

In applicationContext.xml I have

<context:component-scan base-package="com.test.webservices"/>

that scan all classes excepts abstract classes.

Exception is :-


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   

Your abstract class can't be a bean specifically because you can't instantiate it. Remove the annotation from the abstract class and have it only on the concrete class.

The use of @Component annotation when used in combination with

<context:component-scan base-package="com.inn.webservices"/>

Spring tries to create singleton beans for the annotated classes. In your case you should have annotated abstract class. Put your @Component annoation on implementation classes instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM