Question

I need some help with next topic.

I have a hierarchy of beans, just like that:

<bean id="father" class="myPackage.Father" abstract="true"/>  

<bean id="childA" class="myPackage.ChildA" parent="father">    
        <property name="atrib1" value="hello"></property>  
</bean>  

<bean id="childB" class="myPackage.ChildB" parent="father">  
        <property name="atrib2" value="bye"></property>  
        <property name="atrib3" value="other"></property>  
</bean>  

I need to create another bean which contains a reference to the abstract class. Then at run-time, I will need to be able to initialize a variable of TYPE "myPackage.Father" and instantiate it with any of the children classes (the polymorphism mechanism). I mean something like that:

<bean id="reference" class="myPackage.Another"> 
        <property name="atrib4" ref="father"></property>
</bean>  

Note that reference (an instance of myPackage.Another) is not a subclass of myPackage.Father. But with these lines Spring throws the next error:

Bean definition is abstract

How could I deal with it?

I appreciate any help, thanks in advance!

Was it helpful?

Solution 2

First of all abstract bean has nothing to do with the abstract class they don't even need to be mapped to a class.They are used to group the common properties shared by another beans.But you can not use an abstract bean as a ref to wire a property in another bean. If your bean myPackage.Another has a reference of type myPackage.Father and your child beans are assignable (either extends of implements) to myPackage.Father then you can wire either childA or childB in your myPackage.Another bean like this

 <bean id="reference" class="myPackage.Another"> 
            <property name="atrib4" ref="childA"></property>
    </bean>

OTHER TIPS

abstract beans are never instantiated as objects, they exist only in the form of definitions.

From the documention:http://docs.spring.io/spring/docs/3.0.x/reference/beans.html The parent bean cannot be instantiated on its own because it is incomplete, and it is also explicitly marked as abstract. When a definition is abstract like this, it is usable only as a pure template bean definition that serves as a parent definition for child definitions. Trying to use such an abstract parent bean on its own, by referring to it as a ref property of another bean or doing an explicit getBean() call with the parent bean id, returns an error. Similarly, the container's internal preInstantiateSingletons() method ignores bean definitions that are defined as abstract.

In your case

<bean id="reference" class="myPackage.Another"> 
        <property name="atrib4" ref="father"></property>
</bean>

is refering to the bean defined as abstract which is causing the issue.

There is no different between abstract class and abstract beans.When you use beans and refer to another bean which is abstract , it means you want to instantiate an abstract class it and it can not work surely ! Be careful when you are using an abstract class ! An abstract class is used for template class and you can just extend it !

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top