Juliano A. Felipe Juliano A. Felipe - 4 months ago 32
Java Question

'Instantiation of bean failed; Is it an abstract class?' When trying to instantiate a bean with an abstract parent

I'm trying to instantiate a bean, without a class definition, on Spring context by inheriting a parent bean, which is abstract. Like this:

<bean id="childBean" parent="abstractBean">
...
</bean>

<bean id="abstractBean" class="com.java.bean.AbstractBeanClass"
abstract="true" />


But Spring is giving me the following error message:

Error creating bean with name 'childBean' defined in class path resource
[project-spring.xml]:
Instantiation of bean failed; nested exception is
org.springframework.beans.BeanInstantiationException:
Could not instantiate bean class [com.java.bean.AbstractBeanClass]:
Is it an abstract class?;
nested exception is java.lang.InstantiationException
...


I remember having this in another project, and it works perfectly.

I remember having this in the same project, and it works perfectly.

What am I missing here?

UPDATE 1
Found another bean being intatiated exactly like the way I mentioned:

<bean id="variantOptionDataConverter" parent="abstractPopulatingConverter">
...
</bean>


UPDATE 2

Declaration of the Abstract class:

public abstract class AbstractBeanClass<SOURCE, TARGET>
extends AbstractConverter<SOURCE, TARGET>
implements PopulatorList<SOURCE, TARGET>
{
...
}


Note: There's other classes that extends this class. None of the "other spring beans that works" I mentioned above extends this class.

Note 2: I know that it's weird, and, by Java fundamentals, it should not work (like everyone mentioned). But, I don't know how, the other Spring beans are being put in the context. I tried to copy the "other spring beans that work" into my code, change some duplicated names, and it worked. And, that's what I'm trying to understand...

Answer

Found the answer:

Indeed, like Sotirios Delimanolis commented, "You must have something else going on, proxying of some kind maybe", Spring has a proxy that instantiate this type of bean declaration, but it can't instantiate the bean without its methods (which are just declared in the abstract class, not implemented).
So you must "implement" these methods, like this:

<bean id="childBean" parent="abstractBean">
   <lookup-method name="notImplementedMethod" bean="anotherBean"/>
</bean>

The anotherBean must be the same type as notImplementedMethod.