2

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   

Pankaj Verma
  • 39
  • 1
  • 7
  • Create beans only for concrete subclass. Refer http://stackoverflow.com/questions/2921899/spring-abstract-class-and-annotations – Waqar Jun 25 '15 at 08:00

2 Answers2

3

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.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
  • Thanks! In that case how do i use child class parent="" field in xml config for parent abstract class? This condition forces me that my every super class has to be concrete class if i have to use parent="" field in xml config. – Kanagavelu Sugumar Jun 06 '17 at 19:19
0

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.

Bhargav Kumar R
  • 2,190
  • 3
  • 22
  • 38